In this article I will explain with an example, how to implement Remember Me CheckBox functionality in ASP.Net Core Razor Pages.
This article illustrates how to remember the Username and Password for the user when he visits next time using Cookies in ASP.Net Core Razor Pages.
Note: For more details on ASP.Net Core 7 Razor Pages, please refer my article ASP.Net Core 7 Razor Pages: Hello World Tutorial with Sample Program example.
 
 

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

Razor PageModel (Code-Behind)

The PageModel consists of following Handler methods.

Handler Method for handling GET operation

Inside this Handler 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.
 

Handler method for handling POST operation

This Handler method accepts User class object as a parameter.
Inside this Handler 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 Delete method.
Note: For more details on how to Read, Write (Save) and Remove (Delete) Cookies in ASP.Net Core Razor Pages, please refer my article ASP.Net Core Razor Pages: Read, Write (Save) and Remove (Delete) Cookies.
 
Finally, user is redirected to the Razor Page.
public class IndexModel : PageModel
{
    public User User { get; set; }
    private IHttpContextAccessor Accessor;
 
    /// <summary>
    /// Gets HttpRequest cookie collection.
    /// </summary>
    private IRequestCookieCollection Cookies
    {
       get
       {
            return this.Accessor.HttpContext.Request.Cookies;
       }
    }
 
    public IndexModel(IHttpContextAccessor _accessor)
    {
        this.Accessor = _accessor;
    }
 
    public void OnGet()
    {
        this.User = new User();
        if (this.Cookies["UserName"] != null && this.Cookies["Password"] != null)
        {
            this.User.Username = this.Cookies["UserName"];
            this.User.Password = this.Cookies["Password"];
        }
    }
 
    public void OnPostLogin(User user)
    {
        this.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");
        }
 
        RedirectToPage("/Index");
    }
}
 
 

Razor Page(HTML)

Inside the Razor Page, the ASP.Net TagHelpers is inherited.
The HTML of the Razor Page consists of an HTML Table, which consists of a two HTML Input TextBox, CheckBox and a Submit Button.
The Password has been set with password attribute and value has been set with Model property.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostLogin but here it will be specified as Login when calling from the Razor HTML Page.
 
@page
@model RememberMe_Core_Razor.Pages.IndexModel
@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">
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td>UserName:</td>
                <td><input type="text" asp-for="User.Username" /></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" asp-for="User.Password" value="@Model.User.Password" /></td>
            </tr>
            <tr>
                <td>Remember Me:</td>
                <td><input type="checkbox" asp-for="User.RememberMe" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Login" asp-page-handler="Login" /></td>
            </tr>
        </table>
    </form>
</body>
</html>
 
 

Screenshot

ASP.Net Core Razor Pages: Implement Remember Me functionality using CheckBox
 
 

Browser Compatibility

The above code has been tested in the following browsers.
Microsoft Edge  FireFox  Chrome  Safari  Opera
* All browser logos displayed above are property of their respective owners.
 
 

Downloads