回答

2

根據他們的,你可以設置超時的作用,即

HttpContext.Current.Session.Timeout = 20; 

你前面的問題去要動態地做到這一點。您可以在會話中自行存儲和更新時間,併爲基本控制器的每個角色OnActionExecuting進行設置。

if (User.IsInRole("Admin")) 
    { 
     filterContext.HttpContext.Session.Timeout = 
(int)filterContext.HttpContext.Session["AdminTimeoutThatYouSetSomewhereElseGlobally"]; 
    } 
+0

它適用於ASP.NET MVC 5和ASP.NET身份嗎? – 2014-10-20 13:54:18

+1

將適用於Mvc5。你如何實現身份?你有一個基地控制器?應該沒問題。您對用戶的支票可能會有所不同,但會話是相同的。 – dove 2014-10-20 13:57:44

+0

只會使用,如果你使用會議,這是不啓用默認情況下,AFAIK – sobelito 2016-04-07 07:43:04

5

如果您試圖比常規用戶更快地啓動管理員,以下是我在標識中的存根。

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    // other stuff 
    Provider = new CookieAuthenticationProvider 
    { 
     // this function is executed every http request and executed very early in the pipeline 
     // and here you have access to cookie properties and other low-level stuff. 
     // makes sense to have the invalidation here 
     OnValidateIdentity = async context => 
     { 
      // invalidate user cookie if user's security stamp have changed 
      var invalidateBySecirityStamp = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
        validateInterval: TimeSpan.FromMinutes(30), 
        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)); 
      await invalidateBySecirityStamp.Invoke(context); 

      // check if user is in admin role 
      var isAdmin = context.Identity.Claims.Any(c => c.Type == ClaimTypes.Role && c.Value == "AdminRoleName"); 

      // check if enough time has passed to invalidate cookie 
      var currentUtc = DateTimeOffset.UtcNow; 
      if (context.Options != null && context.Options.SystemClock != null) 
      { 
       currentUtc = context.Options.SystemClock.UtcNow; 
      } 

      var issuedUtc = context.Properties.IssuedUtc; 
      var bootThemOut = (issuedUtc == null); 
      if (issuedUtc != null) 
      { 
       var timeElapsed = currentUtc.Subtract(issuedUtc.Value); 
       bootThemOut = timeElapsed > TimeSpan.FromMinutes(3); // invalidate admin cookies in 3 minutes 
      } 

      if (isAdmin && bootThemOut) 
      { 
       context.RejectIdentity(); 
       context.OwinContext.Authentication.SignOut(context.Options.AuthenticationType); 
      } 
     } 
    } 
});    
+1

我發現這篇文章給了我更好的理解上面的答案:ASPNET身份Cookie身份驗證超時超時使用標識和MVC5:http:///www.jamessturtevant.com/posts/ASPNET-Identity-Cookie-Authentication-Timeouts/ – sobelito 2016-04-07 07:35:30