一旦用戶注銷,我就無法使 ASP.NET Core Identity 中的 .AspNetCore.Identity.Application cookie 無效。
一旦用戶單擊注銷,下面的代碼將執行。
public async Task<IActionResult> Logout(LogoutInputModel model)
{
// build a model so the logged out page knows what to display
LoggedOutViewModel loggedOutViewModel = await BuildLoggedOutViewModelAsync(model.LogoutId);
_logger.LogInformation($"loggedOutViewModel : {JsonConvert.SerializeObject(loggedOutViewModel)}");
if (User?.Identity.IsAuthenticated == true)
{
// delete local authentication cookie
await _norskTakstSignInManager.SignOutAsync();
//clear cookies
var appCookies = Request.Cookies.Keys;
foreach (var cookie in appCookies)
{
Response.Cookies.Delete(cookie);
}
// raise the logout event
await _events.RaiseAsync(new UserLogoutSuccessEvent(User.GetSubjectId(), User.GetDisplayName()));
}
// check if we need to trigger sign-out at an upstream identity provider
if (loggedOutViewModel.TriggerExternalSignout)
{
// build a return URL so the upstream provider will redirect back
// to us after the user has logged out. this allows us to then
// complete our single sign-out processing.
string url = Url.Action("Logout", new { logoutId = loggedOutViewModel.LogoutId });
// this triggers a redirect to the external provider for sign-out
return SignOut(new AuthenticationProperties { RedirectUri = url }, loggedOutViewModel.ExternalAuthenticationScheme);
}
return View("LoggedOut", loggedOutViewModel);
}
這成功地清除了瀏覽器中的所有 cookie,但是,如果我在注銷之前獲取名為“.AspNetCore.Identity.Application”的 cookie 的值,然后將其重新添加到瀏覽器中,然后我可以登錄應用程式而無需輸入用戶憑據。

我測驗了幾個以不同方式設定 cookie 過期時間的流程,但似乎沒有一個流程可以正常作業。
我想知道如何在不清除的情況下使 cookie 無效以解決此問題。那么用戶應該無法手動輸入 cookie 并登錄系統。非常感謝任何幫助。謝謝你。
uj5u.com熱心網友回復:
這是設計使然……您可以做的一件事是嘗試在注銷后使用UserManager.UpdateSecurityStampAsync更新用戶的安全標記。
這樣,cookie 的安全標記將與資料庫中的標記不匹配,并且 cookie 將不再有效(但是,不會向該用戶發出其他 cookie,即使他們沒有“退出”......所以如果一個用戶打開了多個會話,所有這些 cookie 都將不再有效,而不僅僅是您退出的那個)。
Identity 不跟蹤特定的用戶會話(它只是針對用戶驗證 cookie,如果匹配,則匹配)。如果您希望能夠有選擇地洗掉會話,則必須自己跟蹤它們
uj5u.com熱心網友回復:
對我來說,最好的安全實踐是將每次登錄和注銷保存在一個記錄中,并使用唯一的隨機 ID 作為 GUID,然后將此“ID 會話”保存到宣告中,并在每次用戶訪問時檢查這一點,如果宣告中的 ID 是正確的到那屆會議。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/371326.html
