In this article, I’m trying to explain how to read and write cookies in asp.net
Cookies may be used for authentication, identification of a user session, user's preferences, shopping cart contents, or anything else that can be accomplished through storing text data. Cookies can also be used for travelling of data from one page to another.
Type of Cookies?
- Persist Cookie - A cookie has not have expired time Which is called as Persist Cookie
- Non-Persist Cookie - A cookie has expired time Which is called as Non-Persist Cookie
Design the form as shown below:
Write the code in the .aspx.cs file
public partial class WebForm1 : System.Web.UI.Page
protectedvoid Page_Load(objectsender, EventArgse)
protectedvoid btnSave_Click(objectsender, EventArgse)
HttpCookie objCookie=new HttpCookie("UserInfo");
objCookie["UserName"] = txtUsername.Text.ToString();
objCookie["Password"] = txtPassword.Text.ToString();
protectedvoid btnRead_Click(objectsender, EventArgse)
Response.Write("Cookies has been Expired");
protectedvoid btnClear_Click(objectsender, EventArgse)
After filling the values in the textbox and click on the save button, cookie will be created on the client machine and the textboxes will be blank as shown below.
If you want to read the cookies, click on the read button
Clear button will clear the value in the textboxes.
You can also read these related post