【问题标题】:Redirect after sign-out through OpenID Connect not working通过 OpenID Connect 注销后重定向不起作用
【发布时间】:2022-10-19 17:55:18
【问题描述】:

我正在通过 OpenID Connect (OIDC) 提供程序使用基于 cookie 的身份验证构建 Blazor Server ASP.NET Core 应用程序。注销时,我想重定向到 localhost URI:https://localhost:44378/signout-oidc。这是在 OIDC 提供商处注册为注销后重定向 URI 的路径。

当我注销时,我通过 Connect 提供商的注销流程发送,他们的日志状态为“结束会话请求验证成功”,但我最终在 URI 上:https://localhost:44378/signout-oidc? state=CfDJ8LdQ[...] 这是一个空白页。如果我在登录时尝试访问 https://localhost:44378/signout-oidc 我会得到Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:错误:远程注销请求被忽略,因为缺少“sid”参数,这可能表示主动注销。

我尝试了无数种不同的组合来覆盖SignedOutRedirectUriSignedOutCallbackPathRemoteSignOutPath 等,以及重定向到其他页面 - 都无济于事。我的想法不多了,希望能提供各种意见。

我添加了配置 OpenID 身份验证的代码:

            services.AddAuthentication(opt =>
            {
                opt.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                opt.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                opt.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            }).AddCookie("Cookies", options =>
            {
                options.Cookie.SameSite = SameSiteMode.None;  
            })
        .AddOpenIdConnect("oidc", options =>
        {
            options.Authority = configuration.GetSection("AuthorizationStrings")["Authority"];
            options.ClientId = configuration.GetSection("AuthorizationStrings")["ClientId"];
            options.ClientSecret = configuration.GetSection("AuthorizationStrings")["ClientSecret"];
            options.ResponseType = "code";
            options.SaveTokens = true;
            options.GetClaimsFromUserInfoEndpoint = true;
            options.UseTokenLifetime = false;
            options.Scope.Add("openid");
            options.Scope.Add("profile");
            options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name" };
            options.SignedOutCallbackPath = "/signout-oidc"; 

通过退出按钮启动的退出流程的代码。重定向 URI 的格式是 OIDC 提供者所期望的(状态参数是可选的,所以我省略了它):

public async Task OnGetAsync()
        {
            var ac = await HttpContext.GetTokenAsync("access_token");
            String uri = String.Format("[CONNECT PROVIDER URI]/endsession?id_token_hint={0}&post_logout_redirect_uri=https://localhost:44378/signout-oidc", ac);            
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            var prop = new AuthenticationProperties
            {
                RedirectUri = uri
            };   
            await HttpContext.SignOutAsync("oidc", prop);
        }

【问题讨论】:

    标签: asp.net authentication cookies single-sign-on openid-connect


    【解决方案1】:

    您可能需要设置 LogoutPath,因为它必须与“Logout”链接的 URL 匹配;如果它们匹配,则 RedirectUri 将被接受。

    }).AddCookie(options =>
    {
        options.LogoutPath = "/User/Logout";
        ...
    });
    

    有关更多详细信息,请参阅此答案 here

    这是我的 /User/Logout 的注销代码

    /// <summary>
    /// Do the logout  
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task Logout()
    {
        await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
    
        //Important, this method should never return anything.
    }
    

    【讨论】:

    • 恐怕这不会改变任何事情 - 存储在 cookie 中的 LogoutPath 是否能够推翻 Post Logout Redirect Uri?我可能对这里发生的流程缺乏一些一般知识
    • 此处不涉及 cookie 本身,并且为了使重定向起作用,我添加了用于将用户注销到我的答案的代码。
    猜你喜欢
    • 1970-01-01
    • 2019-08-04
    • 2019-11-15
    • 2021-03-02
    • 2016-10-28
    • 2023-04-08
    • 1970-01-01
    • 2023-03-10
    • 2016-08-23
    相关资源
    最近更新 更多