.net remember username and password


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default: System.Web. UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
// Read saved cookie information
HttpCookie cookies = Request.Cookies ["USER_COOKIE"];
if (cookies! = Null)
{
/ / If the cookie is not empty, read out the user name and password in the cookie and assign it to the text box at the front desk.
this.txtUserName.Text = cookies ["UserName"];
this.txtPassword.Attributes.Add ("value", cookies ["UserPassword"]);
// Here we still select the option to remember the password.
this.ckbRememberLogin.Checked = true;
}
}
}

protected void ASPxButton1_Click (object sender, EventArgs e)
{
string UserName = txtUserName.Text;
string Password = txtPassword.Text;
// This UserTable is the user information obtained by the data layer.
DataTable UserTable = new UserManager (). GetUserTable (UserName);
//UserTable.Rows.Count>0 indicates that there is a corresponding record in the database, and execution can continue.
if (UserTable.Rows.Count> 0)
{
// If the password obtained from the cookie is the same as the password in the database, then the login is successful
if (UserTable.Rows [0] ["Password"]. ToString () == Password )
{
HttpCookie cookie = new HttpCookie ("USER_COOKIE");
if (this.ckbRememberLogin.Checked)
{
// After all verification information is checked, if the user chooses to remember the password, the user name and password will be written into the cookie and saved .
cookie.Values.Add ("UserName", this.txtUserName.Text.Trim ());
cookie.Values.Add ("UserPassword", this.txtPassword.Text.Trim ());
// Here is the setting of cookie expiration time, here is set a week time, after a week, the state will be automatically cleared.
cookie.Expires = System.DateTime.Now.AddDays (7.0);
HttpContext.Current.Response.Cookies.Add (cookie);
}
else
{
if (cookie ["USER_COOKIE"]! = null)
{
// If the user has no choice Remember the password, then immediately the information in the cookie, and the setting status will remain expired immediately.
Response.Cookies ["USER_COOKIE"]. Expires = DateTime.Now;
}
}
//ScriptManager.RegisterClientScriptBlock(this, this.GetType (), "Script", "<script> alert ('" + ex.Message + "' ) </ script> ", false);

Response.Redirect (" Default.aspx ");

}
}
}
}

 

Reprinted from: https://www.cnblogs.com/wanshutao/p/4286121.html

Guess you like

Origin www.cnblogs.com/wugh8726254/p/12679281.html