2014-02-09 178 views
3

我正在使用OWIN身份驗證的MVC 5。 以下是我的StartUp.cs的代碼。OWIN身份驗證和超時

public void ConfigureAuth(IAppBuilder app) 
    { 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/Account/Login"), 
      ExpireTimeSpan = new TimeSpan(60000000000) 
     }); 
     app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 


    } 

到期時間設置爲60000000000納秒。

現在需求是當cookie過期時,我需要重定向到登錄屏幕。

如何做到這一點?

+1

只是爲了好奇,TimeSpan的單位是100納秒單位,所以應該是60000000000「100納秒」單位。 1納秒= 1/1000000毫秒。 100納秒= 1/10000毫秒 –

回答

3

希望這會幫助別人調試...... 的錯誤是在web.config文件

<system.webServer> 
    <modules> 
    <remove name="FormsAuthenticationModule" /> 
    </modules> 
<system.webServer> 

這裏的命名形式authenticationModule是一個錯字。它應該是

<system.webServer> 
    <modules> 
    <remove name="FormsAuthentication" /> 
    </modules> 
<system.webServer> 

而voilla它開始工作。

0

我發現this例子更加更好:

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

粘貼在從App_Start夾Startup.Auth.cs文件上面的代碼。