Post_Logout_Redirect_Uri 使用 azure 进行身份验证时不重定向

Post_Logout_Redirect_Uri does not redirect when authenticating using azure

使用 OWIN 和 OpenId 对我使用 Azure Active Directory 的基本 Web 应用程序的用户进行身份验证,如 Microsoft 示例项目中的 Readme.md 中所述:https://github.com/Azure-Samples/active-directory-dotnet-webapp-openidconnect

以下项目在我的web.config中:

<add key="ida:PostLogoutRedirectUri" value="https://localhost:44320/" />

Startup.Auth如下:

    private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
    private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
    private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
    private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];

    string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

    public void ConfigureAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,
                RedirectUri = "https://localhost:44320/Home/About",
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthenticationFailed = context => 
                    {
                        context.HandleResponse();
                        context.Response.Redirect("/Error?message=" + context.Exception.Message);
                        return Task.FromResult(0);
                    }
                }
            });
    }
}

然而,microsoft.com 网站不会重定向,即使重定向 URI 已进入 URL

我花了很多时间来正确地弄清楚如何让它工作。即使 3 年前我们只是没有打扰。互联网上似乎有很多人遇到这个问题,我想确保每个人都回答了这个问题。

以下解决方案适用于标准 Azure AD 出现的问题。在使用组织帐户登录的情况下,它也适用于 Azure AD B2C。 (请注意,此解决方案不涵盖新的 b2clogin URI 和用户流技术,因为在我提出问题时它不存在)

  1. 您用作 post 注销重定向的 URI 必须在回复 URL 和 Front Channel 注销 URL 中指定。通常,前端通道注销是 IDP 发送到您的应用程序的静默请求,以允许您使用 运行 逻辑清除 cookie,但不会导致重定向。但是,当指定 post logout redirect 并且必须相同时,它似乎也有作用: 小心不要在 URI 末尾意外添加尾部斜杠。它们在此处和您的客户端应用程序中必须完全相同。

  2. https://login.microsoftonline.com/<org-name>.onmicrosoft.com/V2.0 形式的授权无效。对于标准的 Azure AD,我必须将权限更改为特定租户的 https://login.microsoftonline.com/<tenant-id>/V2.0 形式或 https://login.microsoftonline.com/common/V2.0 形式,我认为这也在某些场景中使用。 对于 B2C AD 租户,它仅适用于上面的 common 端点。

旁注 1:对于 common 端点,您必须在令牌验证参数中将 ValidateIssuer 设置为 false,否则您将遇到异常。正确地进行验证超出了这个 SO 问题的范围。

旁注 2:在 Google 上搜索此问题时,您可能会遇到针对针对 Identity Server 进行身份验证的人的问题的一些解决方案。解决方案和规范建议您将 id_token_hint 添加到注销请求中。对于我来说,要让重定向适用于 Azure AD,这既不充分也没有必要。