为什么Cookie的ExpireTimeSpan设置不起作用? [英] Why doesn't cookie ExpireTimeSpan setting work?

查看:217
本文介绍了为什么Cookie的ExpireTimeSpan设置不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用过:

services.AddAuthenticationCore().ConfigureApplicationCookie(o =>
{
    o.ExpireTimeSpan = TimeSpan.FromHours(1);
    o.SlidingExpiration = true;
});

在ASP.NET Core MVC项目的 Startup.cs 中设置我的身份验证Cookie ExpireTimeSpan .

to set my authentication cookie ExpireTimeSpan in Startup.cs in ASP.NET Core MVC project.

我可以看到登录后已在Web浏览器中正确设置了cookie过期时间,但是即使我每隔10秒刷新一次网站,它也会在每次30分钟后自动注销.

I can see that the cookie expire-time has been set correctly in the web browser after login, but it auto logout after 30 minutes every time, even if I refresh the website every 10 seconds.

如果我将 ExpireTimeSpan 设置为少于30分钟,则可以正确超时,但是不能刷新过期时间.

If I set the ExpireTimeSpan less than 30 minutes, it can timeout correctly, but expire-time cannot be refreshed.

为什么要30分钟?在哪里可以更改30分钟超时设置?还是在IIS中设置?

Why is it 30 minutes? Where can I change the 30 minutes timeout setting? Or is it set in IIS?

推荐答案

为什么要30分钟?

Why is it 30 minutes?

这是ASP.NET Core Identity的默认值.

It's the default of ASP.NET Core Identity.

在哪里可以更改30分钟超时设置?还是在IIS中设置?

Where can I change the 30 minutes timeout setting? Or is it set in IIS?

不.在ConfigureApplicationCookie /Startup/Startup.cs#L42"rel =" nofollow noreferrer> IdentityRegistrar.Register :

No. Call ConfigureApplicationCookie after IdentityRegistrar.Register:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    // ...

    IdentityRegistrar.Register(services);                  // No change
    AuthConfigurer.Configure(services, _appConfiguration); // No change

    services.ConfigureApplicationCookie(o =>
    {
        o.ExpireTimeSpan = TimeSpan.FromHours(1);
        o.SlidingExpiration = true;
    });

    // ...
}

如果在 services.AddIdentity 之前定义它,则您的自定义值将被覆盖."

"If you define it before the services.AddIdentity, your custom values will be overwritten."

https://github.com/aspnet/Identity/issues/1389#issuecomment-324257591

这篇关于为什么Cookie的ExpireTimeSpan设置不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆