c# - 多个子域上的网站 Azure 连接

标签 c# azure azure-active-directory nonce

我们在我们的网络服务器上托管一个网站。该网站需要连接到 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 url 中的“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 更改为用户正在访问的子域?看来,在启动时设置它并不是正确的方法。

最佳答案

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

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

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

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

But changing the RedirectUri in this function doesn't change the 'Redirect_Uri' in the login.microsoftonline.com url

您可以通过将所需的 API 权限和范围委托(delegate)给注册的 Azure AD 应用程序中的 Azure 函数应用程序来执行上述操作。请参阅下面的文档链接供您引用:-

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

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

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

b) 处理回调后(在“parseHash”回调函数上),将用户重定向到子域 URL

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

如果您使用通用登录(而不是上面的嵌入式登录),当用户点击基本域 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

关于c# - 多个子域上的网站 Azure 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72542926/

相关文章:

azure - 同一实例上的多个辅助角色

c# - HMACSHA256 和 HMACSHA512 之间的区别

c# - 列表<>包含问题

c# - 从 LINQ 搜索结果中选择组的第一个元素

azure - Service Fabric 状态可以跨分区和/或集群复制吗?

azure - 如何从 Dynamics CRM Online 对 Azure 托管 Web 服务进行身份验证?

c# - Windows Phone 7 GeoCoordinateWatcher 精度

Azure RBAC 应用程序见解码件贡献者与监视贡献者

azure - 如何使用证书凭据获取 "x5t"的值进行应用程序身份验证

azure - 无法使用 LDAPS 连接到 Azure AD : Error Code 2 - PROTOCOL_ERROR