Form Verification to store the cookie into the user name and password

Source: Internet
Author: User

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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.