C ා asp.net MVC: remember account password with Cookie

Keywords: ASP.NET Database

MVC remembers account password

Use cookie action

Front end:

 1 <div>
 2             User name:<input type="text" id="UserName" value="@ViewBag.UserName"/>
 3         </div>
 4         <div>
 5             &nbsp;&nbsp;&nbsp;Password:<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;Remember password
 9         </div>
10         <div>
11             <input type="button" value="Sign in" onclick="btnRegister()" style="margin-left:100px;margin-top:10px"/>
12         </div>

JS Code:

To transmit data through AJAX, we not only need to transmit the account and password, but also the status of the transmit check box (parameter 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('Login successfully');
 9                     window.location.href = "/OilDrum/Index";
10                 }
11                 else {
12                     alert('Wrong user name or password');
13                 }
14             }
15         })
16     }

 

 

 

In the login method:

The CK parameter is the status of the check box true or false

First, judge whether the account password exists in the database

Then determine whether the check box is selected

Check: create a cookie object

Save the user name and password in the cookie object, set the expiration time of the cookie, and save it to the client

Unchecked: create a cookie object

Judge whether the cookie object is null. If there is a value, set the expiration time to - 1. Because our check box is false and not selected, we need to set the expiration time to the previous time, so that the cookie will be null and saved in the client

 

 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                     //Determine whether to remember the password
14                     if(Ck)
15                     {
16                         HttpCookie hc = new HttpCookie("Example");
17 
18                         //stay cookie Object to save the user name and password
19                         hc["UserName"] = loginModel.Name;
20                         hc["UserPwd"] = loginModel.Password;
21                         //Set expiration time
22                         hc.Expires = DateTime.Now.AddDays(2);
23                         //Save to client
24                         Response.Cookies.Add(hc);
25                     }
26                     else
27                     {
28                         HttpCookie hc = new HttpCookie("Example");
29                         //judge hc Is it empty?
30                         if(hc!=null)
31                         {
32                             //Set expiration time
33                             hc.Expires = DateTime.Now.AddDays(-1);
34                             //Save to client
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         }

 

In the view method in the controller:

Gets whether the cookie object Example created in the UsersLogin method has data

Value: assigned to the corresponding text box

Null: return to view directly

public ActionResult Index()
        {
            //Get data in cookie
            HttpCookie cookie = Request.Cookies.Get("Example");

            //Determine whether the cookie is null
            if(cookie!=null)
            {
                //Assign the saved user name and password to the corresponding text box
                //User name
                var name = cookie.Values["UserName"].ToString();
                ViewBag.UserName = name;
                //Password
                var pwd = cookie.Values["UserPwd"].ToString();
                ViewBag.UserPwd = pwd;
            }
            return View();
        }

Posted by jdashca on Fri, 29 Nov 2019 06:19:41 -0800