How do I log into a site with WebClient?

39,106

If the problem you are having is you can authenticate but you cant keep the authentication cookie here is a cookie aware version of WebClient.

private class CookieAwareWebClient : WebClient
{
    public CookieAwareWebClient()
        : this(new CookieContainer())
    { }
    public CookieAwareWebClient(CookieContainer c)
    {
        this.CookieContainer = c;
    }
    public CookieContainer CookieContainer { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);

        var castRequest = request as HttpWebRequest;
        if (castRequest != null)
        {
            castRequest.CookieContainer = this.CookieContainer;
        }

        return request;
    }
}

EDIT: The link you gave me uses forms authentication with HTTP POST, I don't have the time to walk though it but at least it gives you a start with Google.

Share:
39,106
MonsterMMORPG
Author by

MonsterMMORPG

Hello. I am the only owner and developer of web based online MMORPG game MonsterMMORPG. I am a computer engineer from Turkey and i am currently doing MA at computer engineering. I am specialized with C# & ASP.net. http://www.monstermmorpg.com/ MonsterMMORPG is a Free To Play Browser Based Online Monster MMORPG Game Better Than Online Pokemon Games You will love it's awesome Monsters We have many different unique features. So i suggest you to check them out. Our game is similiar with Pokemon games but it is unique. Like diablo and torch light. You should visit following sites related to us MonsterMMORPG Facebook Pokemon Games Lovers Facebook Application MonsterMMORPG Youtube Channel Monster Game Forum Canavar Oyunu Forum Pokemon Fakemon DeviantArt MonsterMMORPG Google Plus MonsterMMORPG Twitter MonsterMMORPG Review On Browsergamez MonsterMMORPG Review On mmohuts MonsterMMORPG Developer Blog At MMORPG.com MonsterMMORPG Review On onrpg MonsterMMORPG On GameSpot MonsterMMORPG Wiki MonsterMMORPG On 1UP MonsterMMORPG Digg MonsterMMORPG Official Google Plus Page

Updated on November 09, 2020

Comments

  • MonsterMMORPG
    MonsterMMORPG over 3 years

    I want to download something using a WebClient object in C#, but the download domain requires me to be logged in. How can I log in and keep session data using WebClient? I know how to post data with WebClient.