IDX20803:无法从中获取配置

IDX20803: Unable to obtain configuration from

我知道这个问题已经得到解答,但我不明白人们到底在做什么(关于证书、ssl),他们都使用 localhost 而不是我。

我用这个样本作为例子OpenIdConnect

我正在使用:

两者都在使用.Net Core 2.1。 Web 应用程序使用 Azure AD 连接获取 JwtBearer 令牌,该令牌被发送到 API。

看到 API 中的路由 /api/information,Web 应用程序向 API 发送了一个请求,API 返回了上述错误。

准确的错误是:

System.InvalidOperationException: IDX20803: Unable to obtain configuration from: 'https://<mycompany>.onmicrosoft.com/<big Guid of 72 chars>/.well-known/openid-configuration'.

所以我尝试通过添加证书、添加必要的库 (System.Net.Http v4.3.3) 来解决这个错误,检查了 Azure AD 中的每个权限,但是 none 这些都有效。

如果您需要更多信息,我可以将它们添加到此 post。

通过在 appsettings.json 中替换解决了它:

"AzureAd": {
     "Instance": "<APP_Uri_from_Azure_Portal>",
...
}

"AzureAd": {
     "Instance": "https://login.microsoftonline.com/",
...
}

如果有人遇到此问题并且正在使用 Azure B2C,则该实例是您的 Azure b2c 租户的根 url。例如https://myroot.b2clogin.com/。域应为:myroot.onmicrosoft.com/

确保域中没有 https。

这对我有用。

{
    "AzureADB2C": {
        "CallbackPath": "/signin-oidc",
        "ClientId": "<app-registration-app-client-id>",
        "ClientSecret": "<secret-value>",
        "Domain": "<domain-name>.onmicrosoft.com/",
        "EditProfilePolicyId": "B2C_1_profile",
        "Instance": "https://<domain-name>.b2clogin.com/",
        "ResetPasswordPolicyId": "B2C_1_reset",
        "SignUpSignInPolicyId": "B2C_1_signupsignin"
    }
}

如@juunas 在上面的评论之一中所述身份验证处理程序在启动时加载此配置文件以加载其配置以验证身份提供者提供的令牌。

文件路径终点也可以在应用注册部分找到。如果您转到应用程序注册 => 概述 => 端点,然后查找 OpenID Connect 配置端点 (v2)

Startup.cs B2C 身份验证处理程序的代码如下所示,我在 appsettings

中提供了值
services.AddAuthentication(options =>
        {
            options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
        })
          .AddJwtBearer(jwtOptions =>
          {
              jwtOptions.Authority = $"{Configuration["AzureAdB2C:Instance"]}/{Configuration["AzureAdB2C:Tenant"]}/{Configuration["AzureAdB2C:Policy"]}/v2.0/";
              jwtOptions.Audience = Configuration["AzureAdB2C:ClientId"];
              jwtOptions.Events = new JwtBearerEvents
              {
                  OnAuthenticationFailed = AuthenticationFailed
              };
          });

 "AzureAdB2C": {
         "Tenant": "xxxxxxxxxb2c.onmicrosoft.com",
         "Instance": "xxxxxxxxxxxb2c.b2clogin.com",
         "ClientId": "xxxxxx-764c-4e4a-b0ec-xxxxxxf",
         "Policy": "B2C_1_signin1",
         "ScopeRead": "demo.read",

               },

通过在 Startup-->Configure

中添加以下代码解决了该问题
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls 
                                       | SecurityProtocolType.Tls11
                                       | SecurityProtocolType.Tls12
                                       | SecurityProtocolType.Ssl3;

对我来说,这个问题是 firewall/network 相关的 - 当我在浏览器中输入 /.well-known/openid-configuration URL 时,我可以访问它,但是 API 本身不能't(API 和 Identity Server 托管在同一个应用程序中)。 API 托管在 Azure 中,我创建了一个出站安全规则(针对与子网相关联的网络安全组,该子网已集成到 API 所属的我的应用程序服务计划中)阻止 API 调用其应用服务计划中的任何其他应用服务,包括它自己。添加新的出站安全规则以允许修复我的问题(我意识到这导致了与提交者非常相似的错误,但出于不同的原因并且更像是一种边缘情况 - 很可能没有这样的限制设置)。

就我而言,我的 appSettings 中缺少 Authority 密钥。

"AzureAdB2C": {
    "Instance": "https://login.microsoftonline.com",
    "Authority": "https://login.microsoftonline.com/<your tenant id>",
    "ClientId": "...",
    "TenantId": "...",
    "Domain": "https://<your name>.b2clogin.com",
    "SignedOutCallbackPath": "/signout/B2C_1_susi",
    "SignUpSignInPolicyId": "b2c_1_susi",
    "ResetPasswordPolicyId": "b2c_1_reset",
    "EditProfilePolicyId": "b2c_1_edit_profile",
    "CallbackPath": "/signin-oidc"
  }

在我的例子中,https://.b2clogin.com被防火墙拦截了,报错信息一模一样,所以允许URL(通过修改防火墙规则)允许服务器访问 https://xxxx.b2clogin.com 解决了这个问题。希望对你有帮助

添加@AmirAli 的回答。

在 .NET 6 中使用工具 msidentity-app-sync tool 后,我的错误如下所示:

https://github.com/AzureAD/microsoft-identity-web/blob/master/tools/app-provisioning-tool/vs2019-16.9-how-to-use.md

System.InvalidOperationException: IDX20803: Unable to obtain configuration from: 'https://login.microsoftonline.com/<myAD>.onmicrosoft.com/B2C_1_signupsignin/v2.0/.well-known/openid-configuration'

appsettings.json 中的 AzureAd:Instance 从默认的 https://login.microsoftonline.com/ 更改为 https://<myAD>.b2clogin.com/ 然后它起作用了。