简体   繁体   中英

ASP.Net Core Identity login status lost after deploy

I am using ASP.Net Core and MS Identity, I try to understand why after each deployment the login users are logged out. I am running on a IIS 8.5

I have been trying the method in this thread (setting static machine key) ASP.NET Identity 2 relogin after deploy by generating static keys at the server level in IIS UI and adding the following to web.config of the website:

<system.web>
    <machineKey validationKey="XXX"
        decryptionKey="XXX"
        validation="SHA1" decryption="AES"/>
</system.web>

However the problem remains:

  • User logs in
  • Stop site
  • Start site
  • The user needs to log in again

But I also go this:

  • User logs in
  • Restart site
  • The user is still logged in

What can cause the user to be logged off? Any idea on how to avoid that?

(solution split into a separate answer following Chris comment)

I found a solution to keep the login status, it survives website stop/start, and an update of the website source folder:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDataProtection()
            // This helps surviving a restart: a same app will find back its keys. Just ensure to create the folder.
            .PersistKeysToFileSystem(new DirectoryInfo("\\MyFolder\\keys\\"))
            // This helps surviving a site update: each app has its own store, building the site creates a new app
            .SetApplicationName("MyWebsite")
            .SetDefaultKeyLifetime(TimeSpan.FromDays(90));
}

With these additional lines and the machine key set, the login data stays after site stop/start and IIS server restart, and if the site is rebuilt.

More information there: https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview

More proposed by justserega: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?tabs=aspnetcore2x#data-protection

Authentication use Data Protection Stack. If data protection isn't configured, the keys are held in memory and discarded when the app restarts.

If the key ring is stored in memory when the app restarts:

  • All cookie-based authentication tokens are invalidated.
  • Users are required to sign in again on their next request.
  • Any data protected with the key ring can no longer be decrypted. This may include CSRF tokens and ASP.NET Core MVC tempdata cookies.

You have to configure data protection, more information here https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?tabs=aspnetcore2x#data-protection

I was able to solve this issue by going into the site's app pool under advanced settings and set "Load User Profile" to true under Process Model.

Then in Startup.cs use:

services.AddSession(options =>
{
    //Set whatever options you want here
    options.Cookie.IsEssential = true;
    options.IdleTimeout = TimeSpan.FromDays(365);
});

...

app.UseSession();

I'm not positive, but services.AddDistributedMemoryCache(); might be needed, helpful or otherwise useful.

I was trying Session State in IIS and machine keys and much, much, more, yet none was needed for just a basic login session persist through a publish or restart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM