HttpWebRequest and Session Information

  • Thread starter Thread starter ALA
  • Start date Start date
A

ALA

Hi,

does anybody know if it is possible to pass the SessionID with a web
request by using a cookie so that the invoked page in the same domain
can access the session objects of the current user?

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = new CookieContainer();
HttpCookie sessionCookie =
HttpContext.Current.Request.Cookies["ASP.NET_SessionId"];
request.CookieContainer.Add(new Cookie(sessCookie.Name,
sessCookie.Value, "/", "localhost"));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

This example doesn't seem to work for me but I do not know why... at
least I get a timeout when I execute the code above although the
mechanism works for every other cookie type, too. If I start a
HttpWebRequest without any session information, the target page has a
new and different sessionID than the soure page.

Thanks for the help

Andre
 
In theory you create CookieContainer once and use it with every request.
HttpWebRequest will automaticly use it and update if needed.
You do not need to populate/repopulate it every time.

Example:
CookieContainer myContainer = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("login.aspx");
request.CookieContainer = myContainer;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
.......

request = (HttpWebRequest)WebRequest.Create("secondpage.aspx");
request.CookieContainer = myContainer;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();


PS: I belive the cookie's name fro ASP.NET session is changing all the time
(when .NET application restarts)
and it's different than ASP.NET_SessionId
So HttpContext.Current.Request.Cookies["ASP.NET_SessionId"];
should always be null.

George.
 
Back
Top