zoukankan      html  css  js  c++  java
  • C# ASP.NET MVC:使用Cookie记住账号密码

    MVC记住账号密码

    使用cookie操作

    前端:

     1 <div>
     2             用户名:<input type="text" id="UserName" value="@ViewBag.UserName"/>
     3         </div>
     4         <div>
     5             &nbsp;&nbsp;&nbsp;密码:<input type="text" id="UserPwd" value="@ViewBag.UserPwd" style="margin-top:10px"/>
     6         </div>
     7         <div>
     8             <input type="checkbox" id="single" checked="checked" style="margin-left:50px;margin-top:10px"/>&nbsp;记住密码
     9         </div>
    10         <div>
    11             <input type="button" value="登录" onclick="btnRegister()" style="margin-left:100px;margin-top:10px"/>
    12         </div>

    JS代码:

    通过AJAX 传输数据 我们不仅要传输账号和密码 还有传复选框的状态(参数CK)

     1 function btnRegister()
     2     {
     3         $.get("/Login/UsersLogin", { Name: $("#UserName").val(), Password: $("#UserPwd").val(), Ck: $("#single").prop('checked') }, function (data, Status) {
     4             if(Status="success")
     5             {
     6                 if (data > 0)
     7                 {
     8                     alert('登录成功');
     9                     window.location.href = "/OilDrum/Index";
    10                 }
    11                 else {
    12                     alert('用户名或密码错误');
    13                 }
    14             }
    15         })
    16     }

    在登录的方法中:

    CK参数就是复选框的状态true或false

    首先判断数据库中是否存在账号密码

    之后判断复选框是否选中

    选中: 创建一个cookie对象

    在cookie对象中保存用户名和密码,并设置cookie的过期时间,保存到客户端中去

    未选中:创建一个cookie对象

    判断cookie对象中是否空值,存在值的情况下,设置过期时间为-1,因为我们复选框为false没有选中,所以我们要把过期的时间设置为之前的时间,这样Cookie就会空值,保存到客户端中

     注意:

    在将密码存入到cookie中,可能不大安全,因为cookie以文本的形式存放在客户端中,这将会使我们的密码暴露出来,

    我们可以在密码存储到cookie中的时候,对密码进行加密,如(MD5密码加密等....)

     1 public int UsersLogin(LoginModel loginModel,bool Ck)
     2         {
     3             try
     4             {
     5                 List<LoginModel> enumerable = new List<LoginModel>();
     6                 int nn = 0;
     7                 using (IDbConnection con = new SqlConnection(connecString))
     8                 {
     9                     nn =(int) con.ExecuteScalar("select COUNT(1) from Users where Name=@Name and Password=@Password", new { Name = loginModel.Name, Password = loginModel.Password });
    10                 }
    11                 if (nn > 0)
    12                 {
    13                     //判断是否记住密码
    14                     if(Ck)
    15                     {
    16                         HttpCookie hc = new HttpCookie("Example");
    17 
    18                         //在cookie对象中保存用户名和密码
    19                         hc["UserName"] = loginModel.Name;
    20                         hc["UserPwd"] = loginModel.Password;
    21                         //设置过期时间
    22                         hc.Expires = DateTime.Now.AddDays(2);
    23                         //保存到客户端
    24                         Response.Cookies.Add(hc);
    25                     }
    26                     else
    27                     {
    28                         HttpCookie hc = new HttpCookie("Example");
    29                         //判断hc是否空值
    30                         if(hc!=null)
    31                         {
    32                             //设置过期时间
    33                             hc.Expires = DateTime.Now.AddDays(-1);
    34                             //保存到客户端
    35                             Response.Cookies.Add(hc);
    36                         }
    37                         
    38                     }
    39                     return 1;
    40                 }
    41                 else
    42                 {
    43                     return 0;
    44                 }
    45             }
    46             catch (Exception e)
    47             {
    48 
    49                 throw;
    50             }
    51         }

    在控制器中的视图方法中:

    获取UsersLogin方法中创建的cookie对象Example是否存在数据

    有值:赋值给对应的文本框

    空值:直接返回视图

    public ActionResult Index()
            {
                //获取cookie中的数据
                HttpCookie cookie = Request.Cookies.Get("Example");
    
                //判断cookie是否空值
                if(cookie!=null)
                {
                    //把保存的用户名和密码赋值给对应的文本框
                    //用户名
                    var name = cookie.Values["UserName"].ToString();
                    ViewBag.UserName = name;
                    //密码
                    var pwd = cookie.Values["UserPwd"].ToString();
                    ViewBag.UserPwd = pwd;
                }
                return View();
            }
  • 相关阅读:
    使用CustomValidate自定义验证控件
    C#中金额的大小写转换
    Andriod出错之Unable to build: the file dx.jar was not loaded from the SDK folder!
    VC 编写的打字练习
    机房工作笔记Ping只有单向通
    web服务协同学习笔记(1)
    Dll 学习3 将MDI子窗口封装在DLL中
    机房工作学习文件共享
    Andriod出错之Failed to find an AVD compatible with target 'Android 2.2'
    Andriod出错之wrapper was not properly loaded first
  • 原文地址:https://www.cnblogs.com/Hmd528/p/10695156.html
Copyright © 2011-2022 走看看