Wednesday, February 25, 2009

Save and retrieve information from the cookie (specifying an expire time)

Saving information in the cookie and specifying an expire time

protected void LoginButton_Click(object sender, EventArgs e)
{
string username = NameTextBox.Text;

HttpCookie cookie = new HttpCookie("UserInfo");
cookie["Username"] = username;
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);

}


Retrieving information

protected void Page_Load(object sender, EventArgs e)

{
HttpCookie cookie = Request.Cookies["UserInfo"];
if(cookie!=null)
{
string username = cookie["Username"];
if (username != null)
{
Response.Redirect("OrderPage.aspx?Username=" + username);
return;
}
}
Response.Redirect("Login.aspx");
}


No comments:

Post a Comment