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

标签 .net azure asp.net-core asp.net-core-mvc

最近我在 .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/

相关文章:

.net - BlockingCollection(Of T)的目的是什么

.net - 在消息框/对话框上添加单选按钮

Azure 应用服务 - 如何阻止端口 8172 上的 MsDeploy.axd

azure - 使用 CLI 启用 Azure 诊断日志记录

c# - 自定义标签助手 : How do I use complex objects in attributes?

asp.net-core - 带有凭据的 FileServerMiddleware?

c# - 在 C# 中监视文件/目录访问

.net - 从哪里获得 .net 的 apache poi 端口

azure - 无法理解 Azure 的角色 JSON

c# - 将 IdentityServer4 与自定义配置 DBContext 结合使用