ASP.NET身份验证会话超时

8
我使用 ASP.NET Identity .. 我想将会话超时设置为无限或最大值。我尝试了一些方法,但它们没有生效。注意:我使用共享托管。 谢谢。
//web.config
<system.web>
<customErrors mode="Off" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms" />
<sessionState timeout="500000" />
</system.web>

//asp.identiyt config 
public void ConfigureAuth(IAppBuilder app)
{
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        ExpireTimeSpan = TimeSpan.FromDays(365),
        LoginPath = new PathString("/user/login"),
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>
                (
                     validateInterval: TimeSpan.FromDays(365),
                     regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
                     getUserIdCallback: (id) => (Int32.Parse(id.GetUserId()))
                )
        }
    });
}

1
请注意,会话和身份登录并不一定是相同的。此外,试图永久保持会话是一个非常糟糕的想法,而且几乎不可能实现。 - Andrew Barber
1
好的。我想要一个超时的例子,比如一周? - Yargicx
当你登录到比如说YouTube,你会一直保持登录状态,直到你明确登出。我相信一些人问类似问题的时候是想知道如何配置他们的身份提供者以便以这种方式运行。 - undefined
1个回答

7
public void ConfigureAuth(IAppBuilder app)
{            
    var sessionTimeout = 20; // 

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        ExpireTimeSpan = TimeSpan.FromSeconds(30),
    });
}

1
这如何满足“无限或最大值”要求? - Andrew Barber
你不能将会话超时设置为无限,但是你可以根据你的需求在 web.config 中设置超时时间。 - Karthik Devarajan
2
“你不能将会话超时设置为无限。” 那将是有效的和相关的信息要包括。你发布的只是另一种尝试OP已经尝试过的方式,但那并不是他们想要的。 - Andrew Barber
这是最好的答案,至少可以接近“无限”。具体来说,您可以将ExpireTimeSpan设置为以下内容:ExpireTimeSpan = TimeSpan.FromDays(45)这将使Cookie在45天内有效。 - mknopf
我们能假设这行代码 var sessionTimeout = 20; // 只是在这个例子中被意外留下来的吗?它在代码中并没有被使用。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接