2017-08-25 63 views
2

我使用ASP.NET身份创建了一个新的ASP.NET MVC网站。我使用由Visual Studio 2017生成的标准逻辑,并且我选择了个人用户帐户增加登录超时

一切工作正常,只是它似乎在约10至20分钟的不活动状态下注销我,我想保持登录更长的时间。

Googling around后,我发现有关设置CookieAuthenticationOptions.ExpireTimeSpan的信息。但是,使用调试器,我可以看到这个值默认设置为14天。

Startup.Auth.cs:

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
    LoginPath = new PathString("/Account/Login"), 
    Provider = new CookieAuthenticationProvider 
    { 
     OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
      validateInterval: TimeSpan.FromMinutes(30), 
      regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) 
    }, 
    //SlidingExpiration = true,    // Default: true 
    //ExpireTimeSpan = TimeSpan.FromHours(1) // Default: 14 days 
}); 

的Web.Config:

<system.web> 
    <authentication mode="None" /> 
    <compilation debug="true" targetFramework="4.6" /> 
    <httpRuntime targetFramework="4.5.2" executionTimeout="240" maxRequestLength="20480" /> 
    <httpModules> 
    <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 
    </httpModules> 
    <customErrors mode="Off"></customErrors> 
</system.web> 
<system.webServer> 
    <modules> 
    <remove name="FormsAuthentication" /> 
    <remove name="ApplicationInsightsWebTracking" /> 
    <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> 
    </modules> 
    <validation validateIntegratedModeConfiguration="false" /> 
<security> 
    <requestFiltering> 
     <requestLimits maxAllowedContentLength="20971520" /> 
    </requestFiltering> 
    </security> 
</system.webServer> 

所以,没有人知道如何增加的时间量我得到注销由于前不活动?

回答

0

这是web.config文件的配置如下

<system.web> 
    <authentication mode="Forms"> 
    <forms loginUrl="~/SignIn/LoginPage" timeout="2880" defaultUrl="~/Pages/ServicesPage" /> 
    </authentication> 

的超时时间在毫秒

+2

谢谢,但表单身份验证是旧的方式,ASP.NET身份是新的方式。当然,唯一的答案不能是我必须以旧的方式做事。 –

1

财产超时你与isPersistent标志登录界定?

SignInManager.PasswordSignInAsync(string userName, string password, bool isPersistent, bool shouldLockout) 

您需要通过isPersistent = true才能自动再次使用cookie数据登录。

如果您在此处被拒绝,您的身份将在SecurityStampValidator.OnValidateIdentity中刷新,然后它会将您注销。

我建议实施您自己的SecurityStampValidator和UserManager,然后您可以调试为什么它拒绝您在OnValidateIdentity。

还检查您的缓存,也许你只是有一个缓存问题,它看起来像你退出,因为你显示一些“旧”的内容。

+0

我相信这个标志对应于*记住我*复选框。是的,我试着检查。 –

+0

如果将“validateInterval:”设置为几秒钟,会发生什么情况?在那段时间之后它会注销你吗? – 2017-08-25 16:59:50

+0

它没有。但是,让我确定我明白:当您使用*个人用户帐户*设置创建一个MVC应用程序并使用Visual Studio 2017时,您是否说您可以让计算机处于非活动状态,例如一小时或更长时间,以及何时刷新页面或点击你仍然登录的东西? –