gpt4 book ai didi

c# - ASP.NET Core 中的 Cookie 身份验证过期过早

转载 作者:太空狗 更新时间:2023-10-29 19:38:27 28 4
gpt4 key购买 nike

我有一个 ASP.NET Core 1.1.2 项目,我在其中使用 cookie 身份验证。我遇到了一个问题,用户在闲置一个小时或更短时间后被提示重新登录并丢失工作。下面的代码是我在 Startup.cs 的 Configure 函数中使用的代码,据我所知,它应该在至少 8 小时后过期。顺便说一句,ProjectProcessFlow 只是项目的名称。

  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。我需要做什么才能让登录在预期时间过期?

附加信息:

我发现当超时,要求用户重新登录时,服务器上的事件查看器中记录了一个警告,说找不到项目下的logs文件夹。所以我创建了那个文件夹,并等待超时再次发生。发生这种情况时,会在该文件夹中创建一个日志文件,其中包含:

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

当我重复这个过程时,同样的事情发生了,只是在“localhost:”之后出现了一个不同的数字。我应该提到项目名称是 ProjectProcessFlow,但 URL 以 Sprout 结尾。

最佳答案

我知道现在回答这个问题已经太晚了,但对谁来说。IIS 重置池每 20 分钟和每 20 分钟 ASP.NET 生成新 key 以保护 cookie 值(身份验证和 session )。为防止这种情况,将以下代码添加到 Startup 类中的 ConfigureServices

services.AddDataProtection()
.PersistKeysToFileSystem(new System.IO.DirectoryInfo("SOME WHERE IN STORAGE"))
//.ProtectKeysWithCertificate(new X509Certificate2());
.SetDefaultKeyLifetime(TimeSpan.FromDays(90));

完整的指南是 here .一切都是关于 DataProtection

关于c# - ASP.NET Core 中的 Cookie 身份验证过期过早,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45595615/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com