繁体   English   中英

.NET Core 2.0中的持久性Auth cookie

[英]Persistent Auth cookie in .net core 2.0

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

我的网站托管在azure共享池中,我没有指定任何机器密钥

问题。 几个小时不活动(新浏览器)后,我重新打开网站时,我丢失了我的身份验证Cookie,我需要重新登录,但区域性Cookie在上一个会话中仍然有效。

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

LoginController

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);
            });

在此处输入图片说明

您需要使用数据保护来保留会话加密密钥。

一般在Azure App Service或IIS(在VM或本地)中托管应用程序时,IIS会在不活动时回收应用程序和应用程序池。 因此,如果您的应用在特定时间内没有被点击,它将被关闭并在下次连接时重新启动。

发生这种情况时,将为会话生成新的加密密钥,而您之前的会话将无效。

几个小时不活动(新浏览器)后,我重新打开网站时,我丢失了我的身份验证Cookie,我需要重新登录,但区域性Cookie在上一个会话中仍然有效。

您的文化Cookie的价值只是使用Urlencoded。 正如曾先生所说,用于散列和加密的机器密钥可能会在某些时候自动重新生成。 我认为此问题是由您选择的定价层引起的。 对于免费共享层,您的应用程序将在共享基础结构上运行,并且您只有有限的资源(例如CPU时间,RAM,磁盘空间),而没有SLA

应用服务限制

在此处输入图片说明

此外,我尝试重新启动网站并在本地回收应用程序池,身份验证cookie仍可以按预期工作。 对于基本定价层下的Web应用托管,到目前为止,我没有遇到此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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