2016-09-23 28 views
4

我已經實現了asp.net mvc與asp.net身份認證。Asp.net標識 - 重置iis循環中的cookie和會話(重新啓動)

我已經使用基於cookie的身份驗證。在重新啓動IIS/stop併爲我的站點啓動IIS後,當我打開我的網站時,用戶將自動登錄到系統。

用戶cookie未被清除,仍然對用戶有效。如何在重新啓動iis後強制用戶註銷?

我使用了網站的默認樣本。 http://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples

enter image description here

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)) 
       } 
      }); 

回答

0

爲此,我做了一個小把戲。

我們使用會議存儲動態變量ASP.NET MVCasp.net身份進行身份驗證。

  1. 每個請求我已經中斷。
  2. 我檢查過是否asp.net身份有效,會話無效。
  3. 如果會話無效,則使Cookie無效並將用戶導航到特定頁面。

    public class SessionHandler : ActionFilterAttribute 
    { 
        private ApplicationUserManager _userManager; 
        private IAuthenticationManager AuthenticationManager 
        { 
         get 
         { 
          return HttpContext.Current.GetOwinContext().Authentication; 
         } 
        } 
        public ApplicationUserManager UserManager 
        { 
         get 
         { 
          return _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
         } 
         private set 
         { 
          _userManager = value; 
         } 
        } 
        public IIdentity UserIdentity 
        { 
         get { return System.Web.HttpContext.Current.User.Identity; } 
        } 
    
        public override void OnActionExecuting(ActionExecutingContext filterContext) 
        { 
    
         if (!string.IsNullOrWhiteSpace(UserIdentity.GetUserId())) 
         { 
          if (System.Web.HttpContext.Current.Session["Username"] == null) 
          { 
           AuthenticationManager.SignOut(); 
           filterContext.Result = new RedirectToRouteResult(
               new RouteValueDictionary 
               { 
                { "action", "Index" }, 
                { "controller", "Home" } 
               }); 
          } 
         } 
        } 
    } 
    

在Global.asax文件

添加以下代碼

GlobalFilters.Filters.Add(new SessionHandler()); 
+0

所以在登錄時,你總是添加'用戶名'到會話? – trailmax

+0

是的,我們正在使用 –

1

餅乾並不意味着重新啓動IIS時失效 - 這是不是怎麼HTTP協議的作品。在IIS重新啓動時Cookie被無效可能導致生產中出現一些奇怪的行爲 - IIS隨時可能崩潰,或者可能有負載均衡器使用少量IIS服務器爲請求提供服務 - 如果其中一個服務器重新啓動會發生什麼情況?

無論如何,您可以通過在數據庫中批量更新ApplicationUser.SecurityStamp來殺死所有用戶的所有cookie。在Startup.Auth.cs中設置validateInterval: TimeSpan.FromMinutes(2) - 這將在SecurityStamp更新的2分鐘內使所有cookie無效。不建議低於此值 - 這會導致性能問題。

+0

但通常情況下,會議將如果IIS重啓無效。那麼爲什麼不在這裏發生。 –

+0

身份不使用會話 – trailmax

+0

是的你是對的。 :-)但是,即使您使用持票人令牌,重新啓動iis後令牌也將過期。 –