![](/img/trans.png)
[英]How do I add a custom claim to authentication cookie generated by OpenIdConnect middleware in ASP.Net Core after authentication?
[英]How do I invalidate an authentication cookie in ASP.NET Core?
我在使 ASP.NET Core 3.0 中的身份验证 cookie 无效时遇到问题。
设想
我有一个登录网站的用户。 当他们单击注销按钮时,它会调用以下代码:
[HttpGet]
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
HttpContext.Session.Clear();
return RedirectToAction("Index", "Home");
}
这成功地清除了浏览器中的所有 cookie,但是,如果我在.AspNetCore.Cookies
之前获取会话 cookie .AspNetCore.Cookies
的值,然后将其重新添加到以后的请求中,我就可以导航到需要的页面用户认证。
任何人都可以帮助解决这个问题?
注意:最初的问题是关于如何清除用户会话,但我后来意识到这实际上是关于 cookie 本身而不是服务器端会话的问题。
这是我如何执行我的注销方法:
/// <summary>
/// Do the logout
/// </summary>
/// <returns></returns>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Logout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
//Important, this method should never return anything.
}
这里的技巧是让该方法不返回任何内容,因为 SignOut 方法将在内部为浏览器生成自己的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.