如何在注销期间将自定义身份验证属性传递给 openid 连接中间件?

问题描述

我想将一个 url 参数传递给我的 idp 的 endsession 端点。

这就是我尝试这样做的方式:

在我的客户端应用程序的注销操作中,我有

var authprops = new AuthenticationProperties { RedirectUri = postSignoutReturnUrl };
authprops.Dictionary["custom"] = "custom";
HttpContext.GetowinContext().Authentication.SignOut( authprops,OpenIdConnectAuthenticationDefaults.AuthenticationType,CookieAuthenticationDefaults.AuthenticationType);

在我的 openid 连接中间件中,我有

new OpenIdConnectAuthenticationNotifications
            {
                
                RedirectToIdentityProvider = n =>
                {
                    ....

                    if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.logout)
                    {
//the state is null during signout !!!
                        if (n.ProtocolMessage.State != null)
                        {
                            var protectedState = n.ProtocolMessage.State.Split('=')[1];
                            var state = n.Options.StateDataFormat.Unprotect(protectedState);

                            if (state.Dictionary.TryGetValue("custom",out string customParam))
                                n.ProtocolMessage.SetParameter("custom",customParam);
                        }
                    }

                    return Task.CompletedTask;
                },

关于如何以正确的方式做到这一点的任何建议?

解决方法

调用 owinContext.Signout(authenticationProperties,...) 后,authenticationProperties 将通过 owinContext.Authentication.AuthenticationResponseRevoke.Properties 访问。

同样,您可以访问以下 authenticationProperties:

  • owinContext.SignIn(authenticationProperties,...)owinContext.Authentication.AuthenticationResponseGrant.Properties
  • owinContext.Challenge(authenticationProperties,...)owinContext.Authentication.AuthenticationResponseChallenge.Properties

IOwinContext 可以在 RedirectToIdentityProviderNotification.OwinContext 中找到(它是 Microsoft.Owin.Security.Provider.BaseContext 的一部分。