【问题标题】:Sign out from OIDC client not working with IdentityServer4从 OIDC 客户端注销不使用 IdentityServer4
【发布时间】:2021-07-30 17:01:22
【问题描述】:

我目前正在使用 IdentityServer4 开发 .NET 5 应用程序。

我使用授权码 + PKCE 流程登录 - 不幸的是,在 localhost 上注销似乎无法正常工作

我的应用程序环境如下所示:

  • 应用程序(WebApp)
  • IdentityServer4

我在 IdentityServer4 中的客户端定义如下所示:

// Authorization Code + PKCE Flow
new Client
{
    ClientId = "oidcClient",
    ClientName = "Example App",
    ClientSecrets = { new Secret("secret".Sha256()) },

    RedirectUris = { "https://localhost:44301/signin-oidc" },
    PostLogoutRedirectUris = { "https://localhost:44301/signout-callback-oidc" },

    AllowedGrantTypes = GrantTypes.Code,
    RequirePkce = true,
    RequireClientSecret = true,
    
    AllowedScopes = 
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        IdentityServerConstants.StandardScopes.Email,
        IdentityServerConstants.StandardScopes.OfflineAccess,
        "roles",
    },

    AllowPlainTextPkce = false,
},

我在客户端应用上的 OIDC 连接如下所示:

services.AddAuthentication(options => 
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options => 
{
    options.Authority = "https://localhost:5001";
    options.RequireHttpsMetadata = true;
    options.ClientId = "oidcClient";
    options.ClientSecret = "secret";

    options.ResponseType = "code";
    options.UsePkce = true;
    options.ResponseMode = "query";

    options.Scope.Add("offline_access");
    options.Scope.Add("roles");
    
    options.SaveTokens = true;
});

我的 WebApp HomeController 中的注销方法如下所示:

public async Task<IActionResult> Logout()
{
    await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
    await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);

    return new SignOutResult(new[] { OpenIdConnectDefaults.AuthenticationScheme, CookieAuthenticationDefaults.AuthenticationScheme });
}

IdentityServer4 日志告诉我登录 => 登录成功和注销 => 注销成功。

这很奇怪 - 应用程序一直保持登录状态。

当我退出并返回 WebApp 主页索引页面时,我仍然处于登录状态 - 尽管我应该退出。

您知道如何在 IdentityServer4 OIDC 应用程序中正确配置注销吗?

你知道如何解决这个问题吗?

【问题讨论】:

    标签: c# identityserver4 openid-connect .net-5 pkce


    【解决方案1】:

    Logout 方法不应该返回任何东西。因为如果这样做,您将覆盖 SignOut 方法在内部生成的重定向。

    更好的方法是这样做:

    public async Task DoLogout()
    {
        await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
    }
    

    【讨论】:

    • 谢谢,这是一个很好的提示 - 您的代码运行良好!实际上,在我的特殊情况下,当我将 ASP.NET Identity 与 EF Core 用户存储一起使用时,错误似乎仍然存在,但内存中的 TestUser 没有问题......我需要调查一下:)
    • 可能是一些 cookie 混合问题?请注意,身份服务器有自己的一组 cookie,而客户端也有自己的....
    猜你喜欢
    • 2019-11-18
    • 1970-01-01
    • 2017-09-01
    • 2019-12-27
    • 2020-04-10
    • 1970-01-01
    • 2020-04-30
    • 2019-11-23
    • 2021-11-13
    相关资源
    最近更新 更多