【发布时间】:2022-01-06 13:30:03
【问题描述】:
用户注销后,我无法使 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 并登录到系统。非常感谢任何帮助。谢谢。
【问题讨论】:
标签: c# asp.net-core asp.net-identity