当前位置: 移动技术网 > IT编程>开发语言>.net > C# ASP.NET MVC:使用Cookie记住账号密码

C# ASP.NET MVC:使用Cookie记住账号密码

2019年04月13日  | 移动技术网IT编程  | 我要评论

至尊宝v530,555振荡电路,复婚手续

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就会空值,保存到客户端中

 

 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();
        }

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网