2017-08-25 111 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時,您是否說您可以讓計算機處於非活動狀態,例如一小時或更長時間,以及何時刷新頁面或點擊你仍然登錄的東西? –