Pages

Search This Blog

Monday, February 25, 2008

ASP.NET Cookies is domain dependent

Cookies is Domain dependent. Cookies created under subdomain beta.mydomain.com will not able to retrive when access to www.mydomain.com. To ensure that we can retrive the same cookies, we need to specify the domain
For example, we want to write the information to cookies under mydomain when we access to url beta.mydomain.com

HttpCookie cook = new HttpCookie("mydomain");
Response.Cookies.Clear();
Response.Cookies.Add(cook);
cook["UserId"] = userId;
cook["Name"] = username.Text;
cook.Expires = DateTime.MaxValue;
Response.Cookies.Add(cook);

To retrive the cookies information:

if (Request.Cookies["mydomain"] != null)
{
if (Request.Cookies["mydomain"]["UserId"] != null && Request.Cookies["mydomain"]["Name"] != null)
{
Session["UserId"] = Convert.ToInt32(Request.Cookies["mydomain"]["UserId"].ToString());
Session["UserName"] = Request.Cookies["mydomain"]["Name"].ToString();
}
}

3 comments:

Anonymous said...

It is extremely interesting for me to read this article. Thanx for it. I like such topics and anything that is connected to them. I would like to read a bit more on that blog soon.

Anonymous said...

It was certainly interesting for me to read that blog. Thank you for it. I like such themes and everything that is connected to this matter. I definitely want to read a bit more on that blog soon.

Anonymous said...

Don't stop posting such themes. I like to read stories like this. BTW add more pics :)