c# - 身份服务器 4 未删除 cookie
问题描述
我在 angular 5 上有前端应用程序,在 c# 上有使用身份服务器的后端 api。问题是当我单击注销按钮时,令牌被删除,我被重定向到注销页面。
但是,当我尝试刷新主页时,我被重定向到 microsoftonline.com,自动进行身份验证并重定向回主页,我在这里缺少提供用户名和密码,这发生在 chrome incognito 中。
我注意到,如果我从 microsoftonline.com 手动删除 cookie 并重复该过程,这一次我将被要求输入用户名和密码。
所以首先我试着用这种方式清理所有的饼干,但它没有帮助
foreach (var key in HttpContext.Request.Cookies.Keys)
{
HttpContext.Response.Cookies.Append(key, "", new CookieOptions() { Expires = DateTime.Now.AddDays(-1) });
}
下面是我的 accountcontroller 注销方法和 cookie 屏幕
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Logout(LogoutViewModel model)
{
var idp = User?.FindFirst(JwtClaimTypes.IdentityProvider)?.Value;
var subjectId = HttpContext.User.Identity.GetSubjectId();
if (idp != null && idp != IdentityServerConstants.LocalIdentityProvider)
{
if (model.LogoutId == null)
{
model.LogoutId = await interaction.CreateLogoutContextAsync();
}
try
{
await signInManager.SignOutAsync();
}
catch (NotSupportedException)
{
}
}
// set this so UI rendering sees an anonymous user
HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity());
// get context information (client name, post logout redirect URI and iframe for federated signout)
var logout = await interaction.GetLogoutContextAsync(model.LogoutId);
var vm = new LoggedOutViewModel
{
PostLogoutRedirectUri = logout?.PostLogoutRedirectUri,
ClientName = logout?.ClientId,
SignOutIframeUrl = logout?.SignOutIFrameUrl
};
await persistedGrantService.RemoveAllGrantsAsync(subjectId, "angular2client");
return View("LoggedOut", vm);
}
解决方案
如果我理解正确,您是从 IdentityServer4 服务联合到 Microsoft 吗?如果是这样,当您退出身份服务时,您还应该为用户提供退出外部提供商的选项(如果它支持相关功能 - 它需要end_session_endpoint
在发现文档中定义)。
标准 OIDC 中间件支持此功能,因此您应该能够通过调用 SignoutAsync() 并传递 MS 联合登录的方案名称来启动注销。
另一种选择是始终发送prompt=login
您的外部登录请求,然后检查auth_time
您收到的声明。这样您就可以始终强制交互式登录,并验证它何时发生。
推荐阅读
- ckeditor - 将 Vue 组件插入 CKEditor 5 可编辑正文
- android - Unity Android 构建失败
- excel - 在没有循环的情况下结束 .FindNext 包装?
- java - 无法从码头发布请求中检索表单数据
- python - Django 查询过滤器语法问题
- database - 为什么vb停止执行一些代码并跳过它
- android - Android Kotlin coroutine crashing on strict mode
- bash - 嵌套 awk 从第二场比赛中抓取特定字符串并显示
- python - 在调用不带括号的类实例时返回一个对象
- ruby-on-rails - 如何将 Rails 模型文件拆分为多个文件(不是关注点,不是模块,只是单独的文件)?