首页 > 解决方案 > 身份服务器 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);
}

在此处输入图像描述

标签: c#.net-coreidentityserver4

解决方案


如果我理解正确,您是从 IdentityServer4 服务联合到 Microsoft 吗?如果是这样,当您退出身份服务时,您还应该为用户提供退出外部提供商的选项(如果它支持相关功能 - 它需要end_session_endpoint在发现文档中定义)。

标准 OIDC 中间件支持此功能,因此您应该能够通过调用 SignoutAsync() 并传递 MS 联合登录的方案名称来启动注销。

另一种选择是始终发送prompt=login您的外部登录请求,然后检查auth_time您收到的声明。这样您就可以始终强制交互式登录,并验证它何时发生。


推荐阅读