Search This Blog

Saturday, October 13, 2007

ASP.NET Cookies

Cookies is Domain dependent. Cookies created under subdomain will not able to retrive when access to 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

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

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();

No comments: