zoukankan      html  css  js  c++  java
  • (转)ASP.NET里面简单的记住用户名和密码

     using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 using System.Data;
     8 
     9 public partial class _Default : System.Web.UI.Page 
    10 {
    11     protected void Page_Load(object sender, EventArgs e)
    12     {
    13         if (!IsPostBack)
    14         {
    15             //读取保存的Cookie信息
    16             HttpCookie cookies = Request.Cookies["USER_COOKIE"];
    17             if (cookies != null)
    18             {
    19                 //如果Cookie不为空,则将Cookie里面的用户名和密码读取出来赋值给前台的文本框。
    20                 this.txtUserName.Text = cookies["UserName"];
    21                 this.txtPassword.Attributes.Add("value", cookies["UserPassword"]);
    22                 //这里依然把记住密码的选项给选中。
    23                 this.ckbRememberLogin.Checked = true;
    24             }
    25         }
    26     }
    27 
    28     protected void ASPxButton1_Click(object sender, EventArgs e)
    29     {
    30         string UserName = txtUserName.Text;
    31         string Password = txtPassword.Text;
    32         //这个UserTable是数据层获取的用户信息。
    33         DataTable UserTable = new UserManager().GetUserTable(UserName);
    34         //UserTable.Rows.Count>0说明数据库中有对应的记录,可以继续执行。
    35         if (UserTable.Rows.Count > 0)
    36         {
    37             //如果从Cookie里面获取的密码和数据库里面的密码一致则算是登录成功
    38             if (UserTable.Rows[0]["Password"].ToString() == Password)
    39             {               
    40                 HttpCookie cookie = new HttpCookie("USER_COOKIE");
    41                 if (this.ckbRememberLogin.Checked)
    42                 {
    43                     //所有的验证信息检测之后,如果用户选择的记住密码,则将用户名和密码写入Cookie里面保存起来。
    44                     cookie.Values.Add("UserName", this.txtUserName.Text.Trim());
    45                     cookie.Values.Add("UserPassword", this.txtPassword.Text.Trim());
    46                     //这里是设置Cookie的过期时间,这里设置一个星期的时间,过了一个星期之后状态保持自动清空。
    47                     cookie.Expires = System.DateTime.Now.AddDays(7.0);
    48                     HttpContext.Current.Response.Cookies.Add(cookie);
    49                 }
    50                 else
    51                 {
    52                     if (cookie["USER_COOKIE"] != null)
    53                     {
    54                         //如果用户没有选择记住密码,那么立即将Cookie里面的信息情况,并且设置状态保持立即过期。
    55                         Response.Cookies["USER_COOKIE"].Expires = DateTime.Now;
    56                     }
    57                 }
    58                 //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Script", "<script>alert('" + ex.Message + "')</script>", false);
    59 
    60                 Response.Redirect("Default.aspx");
    61 
    62             }
    63         }
    64     }
    65 }
  • 相关阅读:
    python2.7 使用super关键词 报错 TypeError: must be type, not classobj 解决办法
    ACM_高次同余方程
    既然选择了、再怎么艰难也要走下去
    ACM_扩展欧几里德算法
    Codeforces Round #328 (Div. 2)_B. The Monster and the Squirrel
    Codeforces Round #328 (Div. 2)_A. PawnChess
    HDU_5523Game
    POJ_2769同余问题
    poj1258prim算法
    最小生成树—prim算法
  • 原文地址:https://www.cnblogs.com/wanshutao/p/4286121.html
Copyright © 2011-2022 走看看