FormsAuthentication.SetAuthCookie is not setting cookie in WebResponse Cookie Collection

995 Views Asked by At

I have a website that uses forms authentication and calls FormsAuthentication.SetAuthCookie when the user has been validated, i.e FormsAuthentication.SetAuthCookie(userName, false).

I am currently creating HttpWebRequests and calling the website with the username and password and although the credentials pass validation - the response does not have any cokkies in its cookie collection.

The HTTP request/response code is below:

HttpWebRequest request = (HttpWebRequest) CreatePostRequest(baseAddress, postParameters);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

the code gets the response just fine and while debugging I can see the credentials being validated ok and the FormsAuthentication.SetAuthCookie(user, false) being called.

When I check the response cookie collection the count is 0. Can anyone help me understand why this is.

Thanks in advance.

1

There are 1 best solutions below

0
On

You'll most probably find cookies in response.Headers["Set-Cookie"]. You can store these cookies in an instance of CookieContainer class simply by CookieContainer.SetCookies(baseAddress, response.Headers["Set-Cookie"] and then using this CookieContainer instance in your subsequent requests. Hope this helps.