【问题标题】:IdentityServer - pass extra params from endsession endpoint to LogoutIdentityServer - 将额外参数从 endsession 端点传递到 Logout
【发布时间】:2021-07-10 17:16:48
【问题描述】:

当用户未在 IDP SSO 中进行身份验证时,我们如何将额外的参数(我们作为查询参数发送到结束会话端点)传递给注销

我正在使用最新的 Identity Server 4。

在标准情况下,当客户端启动注销(通过访问 endsession 端点)时,一切正常当我们有关于用户的信息(存储在 cookie 中并且 endsession 端点可以成功读取那)。 EndSession 重定向到 https://myidp/Account/Logout?logoutId=someId,我们可以获取在查询字符串中传递给 endsession 端点的任何参数

但是当我们尝试从客户端进行第二次注销时(并且 cookie 中没有经过身份验证的用户),logoutId 参数没有传递到 Logout 端点并且没有机会获得我们的参数在查询字符串中发送到结束会话端点

我们需要这个的原因很简单:

  1. 假设客户在 2 个不同的页面(客户的页面)上单击了两次注销
  2. 当用户注销时,我们希望将其重定向回客户端 URL,或者根据我们发送到 endsession 端点的参数添加一些额外的逻辑。但是由于我们在 Logout 方法中没有得到任何参数 - 我们对客户端以及如何处理此注销请求一无所知

【问题讨论】:

    标签: single-sign-on identityserver4 openid-connect


    【解决方案1】:

    现在,作为解决此问题的方法,我在 .Net Core 应用程序中添加了中间件,它验证了 LogoutId 参数。如果它不存在(我们将无法获取初始参数的情况,因为在查询字符串中没有将 logoutId 传递给 logout 方法),我从我的中间件手动将查询字符串参数添加到重定向的 URL

    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Primitives;
    
    namespace IdentityProviderWebApp.Core
    {
        public class EndRequestMiddleware
        {
            private readonly RequestDelegate _next;
    
            public EndRequestMiddleware(RequestDelegate next)
            {
                _next = next;
            }
    
            public async Task Invoke(HttpContext context)
            {
                await _next(context);
    
                if (context.Request.Path.Value == "/connect/endsession")
                {
                    var logoutUrl = context.Response.Headers["location"].ToString();
    
                    if (!logoutUrl.Contains("?"))
                    {
                        var fixedLocation = context.Response.Headers["location"] + context.Request.QueryString;
    
                        context.Response.Headers.Remove("location");
                        context.Response.Headers.Add("location", new StringValues(fixedLocation));
                    }
                }
            }
        }
    }
    

    注册中间件代码

    app.UseMiddleware<EndRequestMiddleware>();
    

    在 AccountController Logout 方法中获取您期望的变量作为 logout 方法的参数

    public async Task<IActionResult> Logout(string logoutId, string client_id, string redirect_uri)
    

    在 Logout 方法中,从有效上下文(如果 LogoutId 存在)或使用您在 Logout 方法中收到的值获取实际变量

    var logout = await _interaction.GetLogoutContextAsync(logoutId);
    
    clientId = logout.Parameters.Get("client_id") ?? clientId;
    redirectUri = logout.Parameters.Get("redirect_uri") ?? redirectUri;
    

    希望有人能找到更好的方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 2020-08-22
      • 2020-04-26
      • 1970-01-01
      相关资源
      最近更新 更多