1

ich versucht hat, al die Einstellungen auf web.config und UseCookieAuthentication() Methoden zu verwenden, wie zu vielen Themen im Web angezeigt, wie nachstehend aufgeführt:Session-Timeout funktioniert nicht in ASP.NET MVC

Session timeout does not work (is set in web.config)

How to set session timeout in web.config

mvc 5 session timeout after default period (20 mins)

jedoch versuchen, Session-Timeout auf 1 Minute (zum Testen) auf alle Optionen in dieser Konfiguration oder Methoden macht keinen Sinn zu ändern, und ich bin nicht sicher, wo der Fehler ist. Hier sind die Konfigurationen, die ich geändert habe. Irgendeine Idee, um das Problem zu beheben? Ich muss auch geklärt werden, was ist die beste Idee, um Sitzung Zeitüberschreitung in MVC-Anwendung: in web.config oder in Auth-Klassen?

web.config:

<system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5" executionTimeout="60" /> 

    <sessionState mode="InProc" timeout="1" />  

    <!-- For LDAP --> 
    <httpCookies httpOnlyCookies="true" /> 
    <authentication mode="Forms"> 

     <!-- Note: I also remove this part and try with only "sessionState" --> 
     <forms name=".ADAuthCookie" loginUrl="~/Account/Login" timeout="1" 
      slidingExpiration="false" protection="All" /> 
    </authentication> 
</system.web> 


Startup.Auth.cs:

public void ConfigureAuth(IAppBuilder app) 
{ 
    // Code removed for brevity. 

    // Configure the sign in cookie 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
     LoginPath = new PathString("/Account/Login"), 
     Provider = new CookieAuthenticationProvider 
     { 
      // Enables the application to validate the security stamp when the user logs in. 
      // This is a security feature which is used when you change a password or add an external login to your account. 
      OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
       validateInterval: TimeSpan.FromMinutes(1), 
       regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) 
     } 
    });    
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

    app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(1)); 
    app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);   
} 

Antwort

2

Wenn Sie ASP.NET Identity verwenden Sie müssen keine Einstellungen in web.config verwenden. Fügen Sie einfach diese beiden Zeilen zu Ihrem UseCookieAuthentication() Verfahren wie unten dargestellt:


Startup.Auth.cs:

...., 
SlidingExpiration = true, 
ExpireTimeSpan = TimeSpan.FromMinutes(1) 
... 
So

, wird der endgültige Code Ihrer Methode, wie unten gezeigt :

public void ConfigureAuth(IAppBuilder app) 
{ 
    // Code removed for brevity. 

    // Configure the sign in cookie 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
     LoginPath = new PathString("/Account/Login"), 
     Provider = new CookieAuthenticationProvider 
     { 
      // Enables the application to validate the security stamp when the user logs in. 
      // This is a security feature which is used when you change a password or add an external login to your account. 
      OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
       validateInterval: TimeSpan.FromMinutes(30), 
       regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) 
     },    
     SlidingExpiration = true, 
     ExpireTimeSpan = TimeSpan.FromMinutes(1) //Set the session timeout at here 
    });    
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

    app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(1)); 
    app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);   
} 


Für weitere Informationen besuchen Sie bitte ASP.NET-Identity-Cookie-Authentication-Timeouts. Hoffe, das hilft ...