Asp net core 3 1 identity remember me ispersistent false not working on chrome

Introduction

ASP.NET is a popular programming language used for building web applications. It provides a framework for developing dynamic websites and web services. One of the key features of ASP.NET is its ability to handle user authentication and authorization. In this article, we will explore the issue of ASP.NET Core 3.1 Identity's “Remember Me” not working on Chrome and provide a solution to fix it.

The Problem

When using ASP.NET Core 3.1 Identity, the “Remember Me” functionality allows users to stay logged in even after closing the browser. However, some users have reported that this feature is not working properly on the Chrome browser. The session is not persisted, and users are to log in again after closing the browser.

The Solution

To solve this issue, we need to modify the cookie settings in ASP.NET Core 3.1 Identity. By default, the cookie settings are not with Chrome's SameSite attribute requirements. We need to update the cookie options to that the “Remember Me” functionality correctly on Chrome.

To do this, we need to add the code at the beginning of the ASP.NET Core 3.1 Identity code:


services.ConfigureApplicationCookie(options =>
{
    options.Cookie.SameSite = SameSiteMode.None;
    options.Cookie.SecurePolicy = CookieSecurePolicy.;
});

This code configures the application cookie options to set the SameSite attribute to None and the SecurePolicy to Always. The SameSite attribute is set to None to allow the cookie to be sent in cross-site requests, is required for Chrome. The SecurePolicy is set to Always to ensure that the cookie is only sent over HTTPS connections.

Example

Let's take a look at an example to illustrate how to implement this solution:


public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.ConfigureApplicationCookie(options =>
        {
            options.Cookie.SameSite = SameSiteMode.None;
            options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
        });

        // Other  configurations
    }

    // Other startup configurations
}

In this example, we add the cookie configuration code inside the ConfigureServices method of the Startup class. This ensures that the cookie settings are applied during the application's startup .

Conclusion

By updating the cookie settings in ASP.NET Core 3.1 Identity, we can resolve the issue of the “Remember Me” functionality not working on Chrome. The code provided in this article configures the application cookie options to meet Chrome's SameSite attribute requirements. By implementing this solution, users will be able to stay logged in even after closing the browser, enhancing the user experience of your ASP.NET Core 3.1 Identity application.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents