【问题标题】:net core / IdentityServer : unable to logout网络核心/身份服务器:无法注销
【发布时间】:2020-04-16 13:20:37
【问题描述】:

在我的 Blazor 应用程序中,我使用身份服务器 (identityserver.io) 作为外部服务来管理授权。

登录可以正常工作,但是当我尝试注销,然后尝试登录时,我直接登录而无需询问任何用户名/密码!我搜索了一个星期以来没有找到任何解决方案!

我使用以下代码:

    var props = (returnUrl is null) ? null : new AuthenticationProperties()
    {
        RedirectUri = returnUrl
    };

    await HttpContext.SignOutAsync("Cookies");
    await HttpContext.SignOutAsync("oidc", props);

我也试过删除所有的cookies,但是HttpContext.Request.Cookies没有cookie!

我还检查了以下链接: https://mcguirev10.com/2018/01/26/signoutasync-and-identity-server-cookies.html 但我没有看到任何帮助!

仅供参考,这是我的设置方式:

private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
        {
            context.Services.AddAuthentication(options =>
                {
                    options.DefaultScheme = "Cookies";
                    options.DefaultChallengeScheme = "oidc";
                })
                .AddCookie("Cookies", options =>
                {
                    options.ExpireTimeSpan = TimeSpan.FromDays(365);
                    //options.Cookie.Name = ".MyApp";
                })
                .AddOpenIdConnect("oidc", options =>
                {
                    options.Authority = configuration["AuthServer:Authority"];
                    options.RequireHttpsMetadata = true;
                    options.ResponseType = OpenIdConnectResponseType.CodeIdToken;

                    options.ClientId = configuration["AuthServer:ClientId"];
                    options.ClientSecret = configuration["AuthServer:ClientSecret"];

                    options.SaveTokens = true;
                    options.GetClaimsFromUserInfoEndpoint = true;

                    options.Scope.Add("role");
                    options.Scope.Add("email");
                    options.Scope.Add("phone");
                    options.Scope.Add("MyApp");

                    options.ClaimActions.MapAbpClaimTypes();
                });
        }

【问题讨论】:

  • 您没有提到您使用的是 Blazor 客户端还是服务器端,但我最近写了这篇关于 Blazor 的服务器端 OIDC 的文章:mcguirev10.com/2019/12/15/…(以及之后的几篇相关文章那个)。
  • 感谢@McGuireV10,您的示例帮助很大,我正在努力解决这个问题。但是我注意到在您的示例中,当我们注销时,我们不会重定向到所需的页面,而是留在身份服务器上。例如,如果我希望重定向到“index.html”页面,我该怎么做?谢谢

标签: c# .net-core asp.net-identity asp.net-core-webapi identityserver4


【解决方案1】:

您可以尝试这种方式进行注销。它应该工作。

public class LogoutModel : PageModel
    {
        public async Task<IActionResult> OnGetAsync()
        {
            return SignOut(
                new AuthenticationProperties
                {
                    RedirectUri = "/"
                },
                OpenIdConnectDefaults.AuthenticationScheme,
                CookieAuthenticationDefaults.AuthenticationScheme);
        }
    }

【讨论】:

    【解决方案2】:

    要在从 IDS4 注销后通过单击链接 Click here to return to the Interactive client .... 重定向回您的客户端应用程序,您应该在客户端配置中设置正确的 PostLogoutRedirectUris

    new Client
    {
        ....
    
        // where to redirect to after logout
        PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },
    
         ....
    }, 
    

    在@McGuireV10的文章中,他使用身份服务器的演示服务器进行测试,并使用interactive.confidential.short客户端,您不能设置该客户端的PostLogoutRedirectUris,所以默认为https://localhost:5001/signout-callback-oidc。您可以设置自己的身份服务器来测试场景。

    【讨论】:

    • 我尝试了 PostLogoutRedirectUris ,我将它设置为 localhost:5000/index.html ,但它不起作用。我也尝试返回 new RedirectResult("/index.html"); ,但后来我被重定向但没有注销!
    • 身份服务器登出后请挂在链接确认url
    • 嗨@NanYu,你是什么意思?我将 PostLogoutRedirectUris 设置为正确的 URL,但它似乎确实有效!有什么想法可以调试吗?
    • 在您的客户端应用中点击注销后,用户将被重定向到身份服务器的注销页面,有一个href链接显示Click here to return to the Interactive client ....,当点击here时,用户将再次被重定向到客户端应用,PostLogoutRedirectUris 就是那个网址。检查该网址并查看它是否与您的客户端应用程序匹配
    猜你喜欢
    • 2019-03-24
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 2021-08-05
    • 1970-01-01
    • 2017-09-06
    相关资源
    最近更新 更多