asp.net - 使用 IdentityServer 的 Asp Core 客户端全局注销

标签 asp.net asp.net-mvc asp.net-core asp.net-identity identityserver4

在非核心 asp mvc 应用程序中,我有一个用于全局注销用户的 Controller 操作

看起来像这样

   public ActionResult Logout()
    {
        Request.GetOwinContext().Authentication.SignOut();
        return Redirect("/");
    }

现在我有一个 asp 核心客户端,想要注销我试过了

 public async Task<ActionResult> LogOut()
        {
            if (User.Identity.IsAuthenticated)
            {
                await HttpContext.Authentication.SignOutAsync("Cookies");
            }
            return Redirect("/");
        }

更新

现在我好像注销了,但我被重定向到一个需要身份验证的站点。 我可以看到我很快又被重定向到身份服务器,它会自动再次让我进入。

总结: 我在我的 asp 应用程序中注销了,但没有在身份服务器上注销。

如何全局注销?所以我需要在身份服务器上退出?

最佳答案

如果您使用OpenIdConnect 身份验证,您还需要远程注销。尝试更改您的代码,如下所示:

    public async Task<ActionResult> LogOut()
    {
        if (User.Identity.IsAuthenticated)
        {
            await HttpContext.Authentication.SignOutAsync("Cookies");
            await context.Authentication.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties()
            {
                 RedirectUri = "<signed out url>"
            });

            // if you use different scheme name for openid authentication, use below code
            //await context.Authentication.SignOutAsync(<your openid scheme>, new AuthenticationProperties()
            //{
            //   RedirectUri = "/signedout"
            //});
        }
        return Redirect("/");
    }

查看原始样本 https://github.com/aspnet/Security/blob/dev/samples/OpenIdConnectSample/Startup.cs

关于asp.net - 使用 IdentityServer 的 Asp Core 客户端全局注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40161504/

相关文章:

asp.net-mvc - 自定义错误不适用于 MVC4 项目?

javascript - 同域 AJAX 调用被第 3 方身份验证重定向

c# - HTTP 属性与路由相同 - ASP.Net Core API?

asp.net - 合并多个 RSS 源

c# - 将密码存储在 cookie 中是否安全?

c# - ASP.NET C# 和存储过程

c# - 如何在#if 语句中设置.NET Core 进行编译

javascript - 获取子属性包含值的对象

asp.net-mvc - 带有 ASP.NET MVC MapPageRoute 的通配符以支持组织遗留代码

c# - Application_AcquireRequestState 未执行