In this article I will explain with an example, how to implement Remember Me CheckBox functionality i.e. Remember the UserName and Password for the user when he visits next time using
Cookies in ASP.Net MVC.
Model
The Model class consists of following properties:
public class User
{
public string Username { get; set; }
public string Password { get; set; }
public bool RememberMe { get; set; }
}
Controller
The Controller consists of following Action methods.
Action Method for handling GET operation
Inside this Action method,
User class object is created and a check is performed if the
Cookies are not null then, the values of username and password are read from
Cookies and set in
User class object.
Finally, the User class object is returned to the View.
Action method for handling POST operation
This Action method accepts User class object as a parameter.
Inside this Action method, a check is performed if
Remember Me CheckBox is checked then, the username and password is saved in the
Cookies and the expiry date is set to 30 days from the current date time.
And if Remember Me CheckBox is not checked then, the expiry date is set to 1 day in past i.e. -1, so that Cookie will be destroyed.
Finally, user is redirected to the Index View.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
User user = new User();
if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null)
{
user.Username = Request.Cookies["UserName"].Value;
user.Password = Request.Cookies["Password"].Value;
}
return View(user);
}
[HttpPost]
public ActionResult Login(User user)
{
if (user.RememberMe)
{
Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(30);
Response.Cookies["Password"].Expires = DateTime.Now.AddDays(30);
Response.Cookies["UserName"].Value = user.Username;
Response.Cookies["Password"].Value = user.Password;
}
else
{
Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
}
return RedirectToAction("Index");
}
}
View
Inside the View, in the very first line the User class is declared as Model for the View.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Login.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The View also consists of an HTML Table, which consists of TextBoxes and CheckBox created using Html.TextBoxFor, Html.PasswordFor and HTML.CheckBoxFor methods respectively and a Submit Button.
The Html.PasswordFor TextBox has been set with Value attribute.
@model RememberMe_MVC.Models.User
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>UserName:</td>
<td>@Html.TextBoxFor(m => m.Username)</td>
</tr>
<tr>
<td>Password:</td>
<td>@Html.PasswordFor(m => m.Password, new { @Value = Model.Password })</td>
</tr>
<tr>
<td>Remember Me:</td>
<td>@Html.CheckBoxFor(m => m.RememberMe)</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
}
</body>
</html>
Screenshot
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Downloads