【问题标题】:Website Azure connection on mulitple subdomains多个子域上的网站 Azure 连接
【发布时间】:2022-06-10 21:25:45
【问题描述】:

我们正在我们的网络服务器上托管一个网站。该网站需要连接到 Azure/Adfs。用户需要通过 Azure/Adfs 登录才能访问网站的某些部分。

但它只工作了一半。我可以在“customer.nl”上连接,但在“subdomain.customer.nl”上我收到“NONCE 错误”。

有一个“Startup”类,它继承自“UmbracoDefaultOwinStartup”(常规 OwinStartup 的 Umbraco 覆盖)。该类有一个“ConfigureAuth”方法,用于设置配置参数。其中之一是 RedirectUri,它(通过 web.config)设置为“customer.nl”。

“启动”代码:

[assembly: OwinStartup(typeof(Ip.Startup))]
namespace Customername {
    public class Startup : UmbracoDefaultOwinStartup {
        string redirectUri = System.Configuration.ConfigurationManager.AppSettings["RedirectUri"];

        public new void Configuration(IAppBuilder app) {
            ConfigureAuth(app);
            app.MapSignalR();
            base.Configuration(app);
        }

        public void ConfigureAuth(IAppBuilder app) {
            app.SetDefaultSignInAsAuthenticationType(
                CookieAuthenticationDefaults.AuthenticationType);
            app.UseCookieAuthentication(new CookieAuthenticationOptions(){ 
                CookieManager = new SystemWebCookieManager()
            });
            app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions {
                ClientId = clientId,
                Authority = authority,
                RedirectUri = redirectUri,
                PostLogoutRedirectUri = redirectUri,
                Scope = OpenIdConnectScope.OpenIdProfile,
                ResponseType = OpenIdConnectResponseType.IdToken,
                TokenValidationParameters = new TokenValidationParameters() {
                    ValidateIssuer = false
                },
                Notifications = new OpenIdConnectAuthenticationNotifications {
                    AuthenticationFailed = OnAuthenticationFailed
                }
            });
        }
    }
}

如果我尝试登录“subdomain.customer.nl”,我会重定向到 login.microsoftonline.com,但我在 URL 中看到“redirect_url=customer.nl”。

重定向未认证用户的函数是:

public void SignIn(string ReturnUrl = "/") {
    if (!Request.IsAuthenticated) {
        HttpContext.GetOwinContext().Authentication.Challenge(
            new AuthenticationProperties { RedirectUri = ReturnUrl },
            OpenIdConnectAuthenticationDefaults.AuthenticationType);
        }
    }
}

但更改此函数中的 RedirectUri 不会更改 login.microsoftonline.com 网址中的“Redirect_Uri”。

如果我登录 subdomain.customer.nl,我会返回到 customer.nl,并带有以下查询字符串(我已经解码了 URL):

https://www.customer.nl/?errormessage=IDX21323: 
RequireNonce is '[PII is hidden]'. 
OpenIdConnectProtocolValidationContext.Nonce was null, 
OpenIdConnectProtocol.ValidatedIdToken.Payload.Nonce was not null. 
The nonce cannot be validated. 
If you don't need to check the nonce, set OpenIdConnectProtocolValidator.RequireNonce to 'false'. Note if a 'nonce' is found it will be evaluated.

我的猜测是,当 redirect_uri 与 origin-url (subdomain.customer.nl != customer.nl) 不匹配时会弹出 NONCE 错误。

这是正确的吗?如果是这样,我如何将 Redirect_Uri 更改为用户正在访问的子域?在启动时设置它似乎不是要走的路。

【问题讨论】:

    标签: c# azure azure-active-directory nonce


    【解决方案1】:

    • 首先,我建议您确保存在您要通过基本域 URL 连接的子域的公共 DNS 记录,即“customer.nl”。 子域的公共 DNS 记录可以是“A”主机记录、“TXT”记录,但需要在您的公共 DNS 服务器中正确配置,并且如果是独立网络,则需要指向公共 IP 地址应用程序托管在它们上

    • 其次,由于您似乎在您的网站中使用 Azure AD 身份验证 来重定向到子域,我建议您在已注册的 Azure AD 中为相关子域配置重定向 URI用于基础域的应用程序,以便在成功 Azure AD 身份验证后,Web 应用程序会根据需要正确重定向到子域页面

    有关上述内容的更多信息,请参阅以下文档链接:-

    https://docs.microsoft.com/en-us/azure/app-service/configure-authentication-provider-aad

    但在此函数中更改 RedirectUri 不会更改 login.microsoftonline.com url 中的“Redirect_Uri”

    You can do the above by delegating the required API permissions and scope to the Azure function application in your registered Azure AD application。请参考以下文档链接以供参考:-

    https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-permissions-and-consent

    此外,身份验证请求和响应的域需要匹配,因为它存储了用于缓解 CSRF 登录攻击的“nonce”和“state”。因此,我建议您考虑以下针对不同客户端的场景(根据您的重定向机制)并利用 SSO:-

    a) 用户登录到第一个应用程序 (customer.nl)。回调 URL 属于此应用

    b) 处理完回调(在‘parseHash’回调函数上),将用户重定向到子域URL

    c) 当用户登陆子域 URL 应用程序时,应用程序将看到用户没有会话并要求 Azure AD 进行身份验证(authorize () 或 checkSession())。如果用户已经在 Azure AD 中有会话,则不会向用户提示,并且会向应用提供新的身份验证响应

    如果您使用通用登录(与上述嵌入式登录相反),当用户点击基本域 URL (customer.nl) 上的“登录” ) 应用程序,您将用户直接发送到 SPA,指向登录启动端点(例如:-https://app.mydomain.com/login 1),并让子域 URL 应用程序启动实际登录流程。

    有关上述内容的更多信息,请您参考以下链接:-

    https://community.auth0.com/t/log-in-from-different-subdomain-produces-state-error/19116

    【讨论】:

      猜你喜欢
      • 2016-01-24
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多