Remember the username and password and cookie when logging on.

Source: Internet
Author: User

For details about logon, refer to the logon interface of a Forum:

 

You can use cookies to remember this information. For more information about Cookie applications, see
Http://jb51.net/article/33590.htm
Http://jb51.net/article/33591.htm
Now we can simulate a logon interface:
Copy codeThe Code is as follows:
<Table>
<Tr>
<Td style = "width: 15%; text-align: right;">
User Name
</Td>
<Td>
<Asp: TextBox ID = "TextBoxUserName" runat = "server"> </asp: TextBox>
</Td>
</Tr>
<Tr>
<Td style = "text-align: right;">
Password
</Td>
<Td>
<Asp: TextBox ID = "TextBoxPassword" TextMode = "Password" runat = "server"> </asp: TextBox>
</Td>
</Tr>
<Tr>
<Td style = "text-align: right;">
Remember me
</Td>
<Td>
<Asp: CheckBox ID = "CheckBoxRememberMe" runat = "server"/>
</Td>
</Tr>
<Tr>
<Td style = "text-align: right;">
</Td>
<Td>
<Asp: Button ID = "ButtonLogin" runat = "server" Text = "Login" OnClick = "ButtonLogin_Click"/>
</Td>
</Tr>
</Table>

Running effect:

 

We need to determine whether the user selects the Remember me CheckBox when clicking the button. If so, we need to record the login information to the Cookie, set the Cookie expiration time to expire seven days later. Otherwise, only the login information is recorded in the Cookie, and the Cookie expiration time is not set. You can refer to the following logon Event code:
Copy codeThe Code is as follows:
Protected void ButtonLogin_Click (object sender, EventArgs e)
{
Response. Cookies ["Name"]. Expires = DateTime. Now. AddDays (-1 );
Response. Cookies ["Password"]. Expires = DateTime. Now. AddDays (-1 );

If (CheckBoxRememberMe. Checked)
{
Response. Cookies ["Name"]. Expires = DateTime. Now. AddDays (7 );
Response. Cookies ["Password"]. Expires = DateTime. Now. AddDays (7 );
}

Response. Cookies ["Name"]. Value = this. TextBoxUserName. Text. Trim ();
Response. Cookies ["Password"]. Value = this. TextBoxPassword. Text. Trim ();
}

Next, you have to read the Cookie in Page_load.
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
If (Request. Cookies ["Name"]! = Null & Request. Cookies ["Password"]! = Null)
{
This. TextBoxUserName. Text = Request. Cookies ["Name"]. Value;
This. TextBoxPassword. Attributes ["value"] = Request. Cookies ["Password"]. Value;
}
}
}

Check the operation demo. There are three states in the demo. The first is that you do not click CheckBox. In this way, close the window and you will not remember the logon information when you open it again.

The second is to select the CheckBox, so that the next time you open the window, you can also see that the account and password are stored in the corresponding text box, which means the Cookie has not expired.

Third, Click Log On again without clicking the remember me CheckBox. In this way, the system removes the Cookie:

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.