Search code examples
c#cookieshttpwebrequest

Get the .ASPXAUTH cookie value programmatically


Is there a way to get the .ASPXAUTH value programmatically.

Example I login to a website with my own credentials (POST) and then read the response...it does not return the .APSXAUTH in the CookieContainer that I use to track the session.

Anyone has a clue how can I get it and send it with the subsequent gets and posts?

[EDIT] Here's what I do to be more specific:

  • send a HTTP GET to a page. read values like _VIEWSTATE etc.
  • send a HTTP POST to the Login page. It includes the login information.
  • The server sends a 302 response (redirect) to some Default page. The forms authentication cookie is supposed to be included but it's not.

So I was thinking that there might be a better way than this to track session:

CookieContainer _cookieJar = new CookieContainer();

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
request.CookieContainer = _cookieJar;

Solution

  • So the summarize the answer:

    If you're trying to login programatically on a Forms based authentication website trough your own application make sure you follow the steps you take that track the cookies.

    First create a initial GET request, and then do the subsequential POST requests that will do the postback.The request and the responses should be formulated in this way:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
    request.CookieContainer = _cookieJar;
    HttpWebResponse httpsResponse = (HttpWebResponse)request.GetResponse();
    

    The CookieContainer class handles the cookies as expected.

    And if your response is encoded with Gzip just include the following line:

    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    

    before you call request.GetResponse()

    Hope this helps someone out there.