2017-08-09 213 views
6

我有一个ASP.NET Core 1.1.2项目,使用Cookie身份验证。我遇到了一个问题,即用户在闲置一小时或更少时间后会被提示重新登录,并且会失去工作。下面的代码是我在Startup.cs中的Configure函数中使用的,用于设置此功能,并且从我所知道的情况来看,它至少应在8小时后过期。顺便说一句,ProjectProcessFlow只是项目的名称。在ASP.NET核心中Cookie身份验证过期过早

app.UseCookieAuthentication(new CookieAuthenticationOptions() 
    { 
    AuthenticationScheme = "ProjectProcessFlow", 
    LoginPath = new PathString("/Account/Login/"),  
    ExpireTimeSpan = new TimeSpan(8, 0, 0), 
    SlidingExpiration = true, 
    AutomaticAuthenticate = true, 
    AutomaticChallenge = true 
    }); 

我在NuGet中包含Microsoft.AspNetCore.Authentication.Cookies v1.1.2。我需要做什么才能在预期的时间内实现登录到期?

附加信息:

我发现,当超时发生,用户被要求重新登录,警告被记录在事件查看器,它找不到下的日志文件在服务器上该项目。所以我创建了该文件夹,并等待超时再次发生。

Hosting environment: Production 
Content root path: C:\inetpub\wwwroot\Sprout 
Now listening on: http://localhost:13423 
Application started. Press Ctrl+C to shut down. 

当我重复这个过程,同样的事情发生,但不同数量后出现“本地主机:”当发生这种情况,在日志文件中包含这一点,文件夹中创建。我应该提到项目名称是ProjectProcessFlow,但URL以Sprout结尾。

+0

这只是一个猜测,但它可能是相关的,你有没有启用的会议吗?通过'services.AddSession'? –

+0

是的,AddSession和UseSession都在Startup.cs中被调用 – Rono

+0

在你的web.config文件中还应该有一个设置。 –

回答

2

用户在被闲置一小时后会被提示重新登录,并且失去工作。

我有类似的configuration,但它对我来说工作正常。

我能想到的一件事是你不能让Web服务器闲置20分钟。 IIS的应用程序池默认空闲超时是20分钟(我不能说其他Linux网络服务器)

因此,您可以设置较长的应用程序池超时(0表示无穷大),或者从外部服务(如Monitis)每隔5分钟ping一次。

enter image description here

+1

而不是设置您的建议之一,我将空闲超时操作更改为暂停,并解决了我的问题。既然你有我95%的路,你会得到赏金。 – Rono

1

你有services.AddIdentity在ConfigureServices方法设置?

  services.AddIdentity<ApplicationUser, IdentityRole>(config => 
     {    
      // Require a confirmed email in order to log in 
      config.SignIn.RequireConfirmedEmail = true; 
      // Cookie settings 
      config.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromHours(10); 
      config.Cookies.ApplicationCookie.LoginPath = "/Account/LogIn"; 
      config.Cookies.ApplicationCookie.LogoutPath = "/Account/LogOut"; 
     }).AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders(); 

我也有类似的问题,解决了在这里 ASP.NET MVC Core Identity & Cookies