Some verification modules are inevitable for website development. Many modules often have the option "Remember user name and password.
In. net, you can use a class in the system framework to verify the cookie storage value of the user name and password.
Of course, this process is encrypted. The following code is a simple example.
1. Create a mycookie. CS file:
Namespace cookies
{
Public class mycookies
{
Public static httpcookie sendcookie (string name, string PWD)
{
// Create a ticket instance
Formsauthenticationticket ticket = new formsauthenticationticket (1, "ansen", datetime. Now, datetime. Now. adddays (1), true, "userrole ","/");
// Create an encrypted cookie authentication ticket
String hashticket = formsauthentication. Encrypt (ticket );
Httpcookie cookie = new httpcookie (formsauthentication. formscookiename, hashticket );
Cookie. Values ["name"] = Name;
String md5pass = system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (PWD, "MD5 ");
Cookie. Values ["password"] = md5pass;
Cookie. expires = datetime. Now. adddays (1 );
Return cookie;
}
}
}
2. Call on the home page:
Namespace cookies
{
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Initpage ();
}
}
Protected void initpage ()
{
If (session ["ansen"]! = NULL)
{
Response. Write ("the user has logged on! ");
}
Else if (request. Cookies [formsauthentication. formscookiename]! = NULL)
{
String name = request. Cookies [formsauthentication. formscookiename] ["name"]. tostring ();
String Pwd = request. Cookies [formsauthentication. formscookiename] ["password"]. tostring ();
If (Pwd = (system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile ("123456", "MD5 ")))
{
Session ["ansen"] = Name;
Response. Write ("the user logs on from the cookie! ");
Txtname. Text = Name;
Txtpwd. Text = PWD;
}
}
Else
{
Response. Write ("not logged on! ");
}
}
Protected void btnsumbit_click (Object sender, eventargs E)
{
If (txtname. Text = "ansen" & txtpwd. Text = "123456 ")
{
Session ["ansen"] = txtname. text;
Response. Cookies. Add (mycookies. sendcookie (txtname. Text, txtpwd. Text ));
Response. Write ("User Logon successful! ");
}
Else
{
Response. Write ("User Logon Failed! ");
}
}
Protected void btnsignout_click (Object sender, eventargs E)
{
If (request. Cookies [formsauthentication. formscookiename]! = NULL)
{
Response. Cookies. Clear ();
Formsauthentication. signout ();
}
If (session ["ansen"]! = NULL)
{
Session. Abandon ();
}
Response. Redirect ("default. aspx ");
}
Protected void btnsession_click (Object sender, eventargs E)
{
If (session ["ansen"]! = NULL)
{
Session. Abandon ();
}
}
}
}
Default username and password: ansen/123456