Asp.net-Mvc

使舊會話 Cookie 無效 - ASP.Net 身份

  • December 1, 2015

一家外部公司對我正在開發的 ASP.NET MVC 5 應用程序進行了一些滲透測試。

他們提出的一個問題如下所述

與會話管理連結的 cookie 稱為 AspNet.ApplicationCookie。當手動輸入時,應用程序對使用者進行身份驗證。即使使用者從應用程序中註銷,cookie 仍然有效。這意味著,舊會話 cookie 可用於在無限時間範圍內進行有效身份驗證。在插入舊值的那一刻,應用程序接受它並用新生成的 cookie 替換它。因此,如果攻擊者獲得對現有 cookie 之一的訪問權限,則將創建有效會話,並具有與過去相同的訪問權限。

我們正在使用 ASP.NET Identity 2.2

這是我們在帳戶控制器上的註銷操作

[HttpPost]
   [ValidateAntiForgeryToken]
   public ActionResult LogOff()
   {
       AuthenticationManager.SignOut();
       return RedirectToAction("Login", "Account");
   }

在startup.auth.cs

app.UseCookieAuthentication(new CookieAuthenticationOptions
       {
           AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
           LoginPath = new PathString("/Account/Login"),
           ExpireTimeSpan = TimeSpan.FromHours(24.0),
           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, int>(
                validateInterval: TimeSpan.FromMinutes(1.0),
                regenerateIdentityCallback: (manager, user) =>
                    user.GenerateUserIdentityAsync(manager),
                getUserIdCallback: (id) => (Int32.Parse(id.GetUserId())))

           }
       });

我原以為框架會處理使舊會話 cookie 無效但瀏覽 Owin.Security 原始碼的問題,但它似乎沒有。

如何在註銷時使會話 cookie 無效?

編輯 我添加的Jamie DunstanAuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);的建議,但沒有任何區別。我仍然可以從應用程序中註銷,在 Fiddler 中複製先前經過身份驗證的請求,並讓它被應用程序接受。

編輯:我更新的註銷方法

[HttpPost]
   [ValidateAntiForgeryToken]
   public async Task<ActionResult> LogOff()
   {
       var user = await UserManager.FindByNameAsync(User.Identity.Name);

       AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
       await UserManager.UpdateSecurityStampAsync(user.Id);

       return RedirectToAction("Login", "Account");
   }

確保AuthenticationManager.Signout(DefaultAuthenticationTypes.ApplicationCookie);按照 Jamie 的正確建議使用。

能夠再次使用相同的 cookie 登錄是設計使然。Identity 不會創建內部會話來跟踪所有已登錄的使用者,如果 OWIN 獲得命中所有框的 cookie(即來自上一個會話的副本),它會讓您登錄。

如果您在更新安全標記後仍然可以登錄,則很可能 OWIN 無法獲取ApplicationUserManager. 確保你有這條線就在上面app.UseCookieAuthentication

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

或者,如果您使用 DIApplicationUserManager取自 DI:

app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>());

也將值降低validateInterval: TimeSpan.FromMinutes(30)到較低的值 - 我通常會解決幾分鐘。這是 Identity 將 auth-cookie 中的值與數據庫中的值進行比較的頻率。比較完成後,Identity 會重新生成 cookie 以更新時間戳。

引用自:https://stackoverflow.com/questions/34020730