Single sign on IdentityServer-将额外参数从endsession端点传递到注销

Single sign on IdentityServer-将额外参数从endsession端点传递到注销,single-sign-on,identityserver4,openid-connect,Single Sign On,Identityserver4,Openid Connect,当用户未在IDP SSO中进行身份验证时,我们如何传递额外参数(作为查询参数发送到endsession端点)以注销 我正在使用最新的Identity Server 4 在标准情况下,当客户端启动注销(通过访问endsession endpoint)时,当我们有关于用户的信息时(存储在cookie中,endsession endpoint可以成功读取该信息),一切正常。EndSession重定向到https://myidp/Account/Logout?logoutId=someId 并且我们可以

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

我正在使用最新的Identity Server 4

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

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

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

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

  • 目前,作为这个问题的解决办法,我在.Net核心应用程序中添加了中间件,它验证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>();
    
    希望有人能找到更好的方法

    public async Task<IActionResult> Logout(string logoutId, string client_id, string redirect_uri)
    
    var logout = await _interaction.GetLogoutContextAsync(logoutId);
    
    clientId = logout.Parameters.Get("client_id") ?? clientId;
    redirectUri = logout.Parameters.Get("redirect_uri") ?? redirectUri;