I\'m trying to login to a website via my application. What I did:
First I figured out how the browser does the authorization process with Fiddler. I examined how the
CookieContainer
s can hold multiple cookies for different websites, therefor a label (the Domain) has to be provided to bind each cookie to each website. The Domain can be set when instantiating the individual cookies like so:
Cookie chocolateChip = new Cookie("CookieName", "CookieValue") { Domain = "DomainName" };
An easy way to grab the domain to is to make a Uri
(if you aren't using one already) that contains your target url and set the cookie's domain using the Uri.Host
property.
CookieContainer gaCookies = new CookieContainer();
Uri target = new Uri("http://www.google.com/");
gaCookies.Add(new Cookie("__utmc", "#########") { Domain = target.Host });