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 Core.
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 Cookie is removed using the Delete method.
Finally, user is redirected to the Index View.
public class HomeController : Controller
{
private IHttpContextAccessor Accessor;
/// <summary>
/// Gets HttpRequest cookie collection.
/// </summary>
private IRequestCookieCollection Cookies
{
get
{
return this.Accessor.HttpContext.Request.Cookies;
}
}
public HomeController(IHttpContextAccessor _accessor)
{
this.Accessor = _accessor;
}
public IActionResult Index()
{
User user = new User();
if (this.Cookies["UserName"] != null && this.Cookies["Password"] != null)
{
user.Username = this.Cookies["UserName"];
user.Password = this.Cookies["Password"];
}
return View(user);
}
[HttpPost]
public IActionResult Login(User user)
{
if (user.RememberMe)
{
CookieOptions option = new CookieOptions
{
Expires = DateTime.Now.AddDays(30);
};
Response.Cookies.Append("UserName", user.Username, option);
Response.Cookies.Append("Password", user.Password, option);
}
else
{
Response.Cookies.Delete("UserName");
Response.Cookies.Delete("Password");
}
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 following TagHelpers attributes.
asp-action – Name of the Action. In this case the name is Login.
asp-controller – Name of the Controller. In this case the name is Home.
method – 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 a two HTML Input TextBox, CheckBox and a Submit Button.
The Password TextBox has been set with type attribute as password and value has been set with Model property.
@model RememberMe_Core.Models.User
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Login">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>UserName:</td>
<td><input type="text" asp-for="Username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" asp-for="Password" value="@Model.Password" /></td>
</tr>
<tr>
<td>Remember Me:</td>
<td><input type="checkbox" asp-for="RememberMe" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
</form>
</body>
</html>
Screenshot
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Downloads