.netユーザー名とパスワードを記憶する


システムの使用;
System.Collections.Genericの使用;
System.Linqの
使用;
System.Webの
使用; System.Web.UIの使用; System.Web.UI.WebControlsの使用;
System.Dataの使用;

パブリック部分クラス_Default:System.Web UI.Page
{
protected void Page_Load(object sender、EventArgs e)
{
if(!IsPostBack)
{
//保存されたcookie情報を
読み取るHttpCookie cookies = Request.Cookies ["USER_COOKIE"];
if(cookies!= Null)
{
/ / Cookieが空でない場合は、Cookieのユーザー名とパスワードを読み取り、フロントデスクのテキストボックスに割り当てます。
this.txtUserName.Text = cookies ["UserName"];
this.txtPassword.Attributes.Add( "value"、cookies ["UserPassword"]);
//ここでは、パスワードを記憶するオプションを選択しています。
this.ckbRememberLogin.Checked = true;
}
}
}

protected void ASPxButton1_Click(object sender、EventArgs e)
{
string UserName = txtUserName.Text;
string Password = txtPassword.Text;
//このUserTableは、データレイヤーによって取得されたユーザー情報です。
DataTable UserTable = new UserManager()。GetUserTable(UserName);
//UserTable.Rows.Count>0は、データベースに対応するレコードがあり、実行を続行できることを示します。
if(UserTable.Rows.Count> 0)
{
// Cookieから取得したパスワードがデータベース内のパスワードと同じ
場合、ログインは成功しますif(UserTable.Rows [0] ["Password"]。ToString()== Password )
{
HttpCookie cookie = new HttpCookie( "USER_COOKIE");
if(this.ckbRememberLogin.Checked)
{
//すべての検証情報がチェックされた後、ユーザーがパスワードを記憶することを選択した場合、ユーザー名とパスワードがCookieに書き込まれて保存されます。
cookie.Values.Add( "UserName"、this.txtUserName.Text.Trim());
cookie.Values.Add( "UserPassword"、this.txtPassword.Text.Trim());
// Cookieの有効期限の設定です。ここでは、1週間の時間を設定します。1週間後、状態は自動的にクリアされます。
cookie.Expires = System.DateTime.Now.AddDays(7.0);
HttpContext.Current.Response.Cookies.Add(cookie);
}
else
{
if(cookie ["USER_COOKIE"]!= null)
{
//ユーザーに選択肢がない場合パスワードを覚えてから、すぐにクッキーの情報を保存すると、設定ステータスはすぐに期限切れのままになります。
Response.Cookies ["USER_COOKIE"]。Expires = DateTime.Now;
}
}
//ScriptManager.RegisterClientScriptBlock(this、this.GetType()、 "Script"、 "<script> alert( '" + ex.Message + "' )</ script> "、false);

Response.Redirect(" Default.aspx ");

}
}
}
}

 

転載:https://www.cnblogs.com/wanshutao/p/4286121.html

おすすめ

転載: www.cnblogs.com/wugh8726254/p/12679281.html