c# - Azure AD 联合注销未重定向到客户端应用程序

标签 c# azure logout thinktecture-ident-server identityserver3

我正在使用Identity Server 3用于我正在构建的 .Net MVC Web 应用程序的中央身份验证服务器。

我已将身份验证服务器配置为使用 Open ID Connect 身份提供程序,以便允许用户使用混合流针对 Multi-Tenancy Azure Active Directory 帐户进行身份验证。

目前,登录工作按预期进行,我的客户端应用程序重定向到身份验证服务器,身份验证服务器又重定向到 Microsoft 进行登录,然后使用正确填充的访问 token 返回到我的客户端应用程序。

但是,当我尝试注销时,我会正确重定向到 Microsoft,但页面返回到身份验证服务器时会停止,而不是继续返回到我的客户端应用程序。

我相信我已经按照概述正确设置了注销后重定向 here认为我的所有设置都没有问题。

当我拉下 Identity Server 3 代码并调试它时,它正确地将 signOutMessageId 设置到查询字符串上,但在 UseAutofacMiddleware 方法中遇到以下错误当它尝试重定向到我映射的 signoutcallback 位置时:

Exception thrown: 'System.InvalidOperationException' in mscorlib.dll

Additional information: Headers already sent

我的身份验证服务器设置:

app.Map("identity", idsrvApp => {
    var idSvrFactory = new IdentityServerServiceFactory();

    var options = new IdentityServerOptions
    {                
        SiteName = "Site Name",
        SigningCertificate = <Certificate>,
        Factory = idSvrFactory,
        AuthenticationOptions = new AuthenticationOptions
        {
            IdentityProviders = ConfigureIdentityProviders,
            EnablePostSignOutAutoRedirect = true,
            PostSignOutAutoRedirectDelay = 3
        }
    };
    idsrvApp.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    idsrvApp.UseIdentityServer(options);

    idsrvApp.Map("/signoutcallback", cb => {
                    cb.Run(async ctx => {
                               var state = ctx.Request.Cookies["state"];
                               ctx.Response.Cookies.Append("state", ".", new Microsoft.Owin.CookieOptions { Expires = DateTime.UtcNow.AddYears(-1) });
                               await ctx.Environment.RenderLoggedOutViewAsync(state);
                    });
                });
});

用于连接到 Azure AD 的“我的 Open Id Connect”设置:

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions
    {
        AuthenticationType = "aad",
        SignInAsAuthenticationType = signInAsType,

        Authority = "https://login.microsoftonline.com/common/",
        ClientId = <Client ID>,
        AuthenticationMode = AuthenticationMode.Active,
        TokenValidationParameters = new TokenValidationParameters
        {
            AuthenticationType = Constants.ExternalAuthenticationType,
            ValidateIssuer = false,
        },
        Notifications = new OpenIdConnectAuthenticationNotifications()
        {
            AuthorizationCodeReceived = (context) =>
            {
                var code = context.Code;
                ClientCredential credential = new ClientCredential(<Client ID>, <Client Secret>);
                string tenantId = context.AuthenticationTicket.Identity.FindFirst("tid").Value;
                AuthenticationContext authContext = new AuthenticationContext($"https://login.microsoftonline.com/{tenantId}");
                AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
                            code, new Uri(<Identity Server URI>/aad/"), credential, "https://graph.windows.net");

                return Task.FromResult(0);
            },
            RedirectToIdentityProvider = (context) =>
            {
                string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase;
                context.ProtocolMessage.RedirectUri = appBaseUrl + "/aad/";
                context.ProtocolMessage.PostLogoutRedirectUri = appBaseUrl + "/signoutcallback";

                if (context.ProtocolMessage.RequestType == Microsoft.IdentityModel.Protocols.OpenIdConnectRequestType.LogoutRequest)
                {
                    var signOutMessageId = context.OwinContext.Environment.GetSignOutMessageId();
                    if (signOutMessageId != null)
                    {
                        context.OwinContext.Response.Cookies.Append("state", signOutMessageId);
                    }
                }
                return Task.FromResult(0);
            }
    });

我找不到有关此问题的原因或解决方案的任何信息。如何配置它以正确重定向回我的客户端应用程序?

编辑:

GitHub上的相关讨论:https://github.com/IdentityServer/IdentityServer3/issues/2657

我也在 MyGet 上的最新版本的 Identity Server (v2.4.1-build00452) 上尝试过此操作,但遇到了同样的问题。

我还创建了一个存储库,可以在此处重现该问题:https://github.com/Steve887/IdentityServer-Azure/

我的 Azure AD 设置:

enter image description here

最佳答案

我相信您遇到了一个错误,该错误已在 2.5 中修复(截至今天尚未发布):https://github.com/IdentityServer/IdentityServer3/issues/2678

关于c# - Azure AD 联合注销未重定向到客户端应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35737045/

相关文章:

azure - 如何从 RoleEntryPoint.OnStart() 获取 WebRole 站点根路径?

swift - FBLogOut : ios how to override calling UIActionSheet While FBLogOut in FBSDKLoginButtion

c# - UWP - 面板,最后一个元素自动高度

c# - Lambda 表达式对比不同的对象

azure - 如何在办公时间开启/关闭云实例

azure - 无需开发人员即可更新 ServiceConfiguration.cscfg 文件

c# - ASP NET session 使用 .ToString() 抛出错误

c# - 从 SQL 返回 2 个结果

session - JSF2 + CDI @Produces 保持旧的 session 状态

Django 的注销功能删除语言环境设置