gpt4 book ai didi

.net - .net core 2.0 中的持久身份验证 cookie

转载 作者:行者123 更新时间:2023-12-03 05:51:23 25 4
gpt4 key购买 nike

最近我在 .net core 2.0 中创建了新网站,并在身份验证中使用持久 cookie。我还在语言中使用持久文化 cookie。

我的网站托管在 azure 共享池中,我没有指定任何计算 secret 钥

问题。当我在几个小时不活动(新浏览器)后重新打开我的网站时,我丢失了我的身份验证 cookie,我需要再次登录,但文化 cookie 按照上次 session 工作。

我还设置了Application Insights 可用性来预热我的应用程序(每 10 分钟从 2 个不同位置 ping 一次网站)。

登录 Controller

if (this.accountService.ValidateOTP(phoneNumber, otp))
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.MobilePhone, phoneNumber),
new Claim(ClaimTypes.Name, phoneNumber)
};
var userIdentity = new ClaimsIdentity("Custom");
userIdentity.AddClaims(claims);
ClaimsPrincipal userPrincipal = new ClaimsPrincipal(userIdentity);

//await HttpContext.SignOutAsync("AnimalHubInstance");
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
userPrincipal,
new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTime.Now.AddYears(1),
});
}

启动

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(option =>
{
option.LoginPath = new PathString("/Account/Unauthorized");
option.LogoutPath = new PathString("/Account/Logout");
option.Cookie.Name = ".myAuth";
option.ExpireTimeSpan = TimeSpan.FromDays(365);
option.Cookie.Expiration = TimeSpan.FromDays(365);
});

enter image description here

最佳答案

您需要使用data protection保留您的 session 加密 key 。

一般情况下,当在 Azure 应用服务或 IIS 中(在虚拟机或本地)中托管应用时,IIS 将在不活动时回收应用和应用池。因此,如果您的应用程序在特定时间内没有受到点击,它将被关闭并在下次连接时重新启动。

发生这种情况时,将为 session 生成新的加密 key ,并且您之前的 session 将无效。

关于.net - .net core 2.0 中的持久身份验证 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47558231/

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