【问题标题】:ASP.NET Identity Session TimeoutASP.NET 身份会话超时
【发布时间】:2014-11-19 21:52:53
【问题描述】:

我使用 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()))
                )
        }
    });
}

【问题讨论】:

  • 请注意,Session 和身份登录不一定是一回事。还;试图“永远”保持会话是一个非常非常糟糕的主意。而且几乎不可能。
  • 好的。例如,我想要一周的超时时间?

标签: asp.net session asp.net-identity


【解决方案1】:
public void ConfigureAuth(IAppBuilder app)
{            
    var sessionTimeout = 20; // 

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

【讨论】:

  • 这如何满足“无限或最大值”的要求?
  • 您不能将会话超时设置为无限制,但您可以根据您在网络配置中的要求设置超时
  • “您不能将会话超时设置为无限制” 将是有效的相关信息。您发布的内容只是另一种尝试 OP 已经在尝试的方式,这并没有达到他们想要的效果。
  • 这是最好的答案,至少可以接近“无限”。具体来说,您将 ExpireTimeSpan 设置为如下所示:ExpireTimeSpan = TimeSpan.FromDays(45) 这将允许 cookie 有效期为 45 天
猜你喜欢
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 2019-07-26
  • 2011-10-18
  • 1970-01-01
相关资源
最近更新 更多