1. 程式人生 > >C# ASP.NET MVC:使用Cookie記住賬號密碼

C# ASP.NET MVC:使用Cookie記住賬號密碼

過期 lse ring rop fun 客戶端 mvc click count

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 [email protected] and [email protected]", 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();
        }

C# ASP.NET MVC:使用Cookie記住賬號密碼