First, verify whether the cookie contains data in the loading event:
// If the cookie is not empty, the data is filled in text.
If (request. Cookies ["username"]! = NULL ){
This.txt username. Text = request. Cookies ["username"]. value;
This.txt PWD. Text = request. Cookies ["PWD"]. value;
}
Write code in a logon event: it will expire in 10 minutes
String name = this.txt username. text;
String userpwd = this.txt PWD. text;
If (name = "admin" & userpwd = "123 ")
{
Httpcookie cookie1 = new httpcookie ("username", name );
// Set the expiration time
Cookie1.expires = datetime. Now. addminutes (10 );
// Store it in cookies
Response. Cookies. Add (cookie1 );
Httpcookie cookie2 = new httpcookie ("PWD", userpwd );
Cookie2.expires = datetime. Now. addminutes (10 );
Response. Cookies. Add (cookie2 );
Clientscript. registerstartupscript (this. GetType (), "", "alert ('logon successful, data already saved in cookier')", true );
Write code to the deletion event:
// How to delete a cookie stored on the client:
// 1. Create a cookie with the same name as the cookie saved on the client (objective: to overwrite the cookie with the corresponding name on the client)
// 2. Set the cookie expiration time to the current time (objective: to overwrite the cookie with the corresponding name of the client and immediately expire on your own)
// 3. Save the cookie to the client through response
// Suicide Deletion
Httpcookie cookie1 = new httpcookie ("username", null); // overwrite data in the original text box with null
// Set the effective time to the current time
Cookie1.expires = datetime. now;
Response. Cookies. Add (cookie1 );
Httpcookie cookie2 = new httpcookie ("PWD", null );
// Set the effective time to the current time
Cookie1.expires = datetime. now;
Response. Cookies. Add (cookie2 );
Clientscript. registerstartupscript (this. GetType (), "", "alert ('deleted successfully')", true );
Cookie Storage Data