首页 新闻 会员 周边

asp.net core 2.1 网站重启 cookie 登录失效问题?

0
悬赏园豆:20 [已解决问题] 解决于 2019-01-14 19:45

网站是用 asp.net core 2.1 编写的。

登录代码

ClaimsPrincipal principal = new ClaimsPrincipal(identity);
                    var authProperties = new AuthenticationProperties { };
                    if (isRemember)
                    {
                        authProperties = new AuthenticationProperties
                        {
                            ExpiresUtc = DateTimeOffset.UtcNow.AddDays(7),
                            IsPersistent = true
                        };
                    }
                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, authProperties);

startup.cs: ConfigureServices

 services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie(options =>
            {
                options.LoginPath = new PathString("/Cn/Login");
                options.Cookie.Name = ".demo";
                options.Cookie.HttpOnly = true;
            });

startup.cs: Configure

app.UseAuthentication();

本地测试都正常,七日之内都正常自动登录。但是、发布到服务器后 iis 下, 虽然 http 响应头信息带 之前登录的cookie信息, 只要重启该网站应用程序,登录失效,要求重新登录。这个怎么破?难道服务器环境要配置其他信息吗?

ps: 服务器环境 windows server 2012 r2 + IIS + .net core 2.1

Go_Start的主页 Go_Start | 初学一级 | 园豆:184
提问于:2019-01-04 22:26
< >
分享
最佳答案
0

AddCookie 设置一下 options.ExpireTimeSpan 试试

收获园豆:20
dudu | 高人七级 |园豆:30943 | 2019-01-04 22:36

可能是因为没有设置 AddDataProtection ,比如

services.AddDataProtection(options => options.ApplicationDiscriminator = "xxx")
                .PersistKeysToRedis(redisConn, "DataProtection-Keys-xxx");
dudu | 园豆:30943 (高人七级) | 2019-01-04 22:39

试过了,还是不行。 而且AddDataProtection 也是设置的。只要该网站应用程序重启,登录状态失效。真是纳闷了

Go_Start | 园豆:184 (初学一级) | 2019-01-07 13:26

@Go_Start: 建议看一下日志是否有对应的告警

dudu | 园豆:30943 (高人七级) | 2019-01-08 18:00

@dudu: 谢谢,就是 AddDataProtection 配置问题。之前没有搞懂。感谢。

Go_Start | 园豆:184 (初学一级) | 2019-01-14 19:41
其他回答(1)
0

解决了吗, 我这也是, 开发了一个游戏, 更新肯定会有, 可每次更新或回收应用程序池, 登录状态都会失效

Dodu.Net | 园豆:204 (菜鸟二级) | 2019-01-13 10:22

解决了,@dudu 正解。

请参考:

https://docs.microsoft.com/zh-cn/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-2.2

支持(0) 反对(0) Go_Start | 园豆:184 (初学一级) | 2019-01-14 19:42

@Go_Start:
你好, 这篇文章我有些看不太明白, 我试着SetDefaultKeyLifetime, 还是不行, 你是怎么做的呢?

支持(0) 反对(0) Dodu.Net | 园豆:204 (菜鸟二级) | 2019-01-16 09:32

@Go_Start: 万分谢谢, 已经成功了..

支持(0) 反对(0) Dodu.Net | 园豆:204 (菜鸟二级) | 2019-01-19 16:48

@Dodu.Net: 贴代码呀

支持(0) 反对(0) Payson | 园豆:204 (菜鸟二级) | 2019-08-19 18:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册