【问题标题】:System.InvalidOperationException: IDX20803 when validating Azure AD tokenSystem.InvalidOperationException:验证 Azure AD 令牌时出现 IDX20803
【发布时间】:2020-02-18 02:40:00
【问题描述】:

我在 Angular 中有一个 SPA 客户端,它成功登录到 Azure AD 并生成一个令牌,然后我通过 Authorization: Bearer ${token} 将其传递给 .Net Core(3.0) API,然后我尝试使用 [Authorize] 验证令牌以检查然后允许访问 REST Api 调用,我们仍在使用 "ver": "1.0" 并且 JWT 标头中没有随机数。我在 ConfigureServices 方法中有以下代码:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
               .AddJwtBearer(options =>
               {
                   options.Authority = "https://login.microsoftonline.com/common";
                   options.RequireHttpsMetadata = false;
                   options.TokenValidationParameters = new TokenValidationParameters
                   {
                       ValidateIssuer = true,
                       ValidIssuer = options.Authority,
                       ValidateLifetime = true,
                       ValidateAudience = true,
                       ValidAudience = "{myClientID}",
                       IssuerValidator = (issuer, token, parameter) => "https://sts.windows.net/{MyIssuer}/"
                   };
               });

            services.AddAuthorization(options =>
            {
                options.DefaultPolicy =
                     new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme)
                    .RequireAuthenticatedUser()
                    .Build();
            });

当我使用 locahost 运行项目时,它工作正常,但我将代码部署到服务器,我收到以下错误消息:

Exception occurred while processing message.

Exception: 
System.InvalidOperationException: IDX20803: Unable to obtain configuration from: 'https://login.microsoftonline.com/common/.well-known/openid-configuration'.
 ---> System.IO.IOException: IDX20804: Unable to retrieve document from: 'https://login.microsoftonline.com/common/.well-known/openid-configuration'.
 ---> System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it.
 ---> System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it.
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---

上面的消息表明某些东西不允许我连接到站点进行验证,我的参数是否正确?我使用 jwt.io 来检查令牌,它说签名是有效的,在正文上我可以看到我对 "aud:","iss:" "amr":["pwd"] 等的期望值。我是 Azure AD 和令牌验证的新手,所以请多多包涵。有什么我想念的吗?谢谢

【问题讨论】:

  • 此错误是由于您的 API 无法加载 OpenID 发现文档 (/.well-known/openid-configuration) 导致的,您是否在服务器上设置了防火墙?

标签: jwt azure-active-directory bearer-token .net-core-3.0


【解决方案1】:

该错误是由应用无法连接到 Azure AD 元数据终结点引起的。 它需要一开始就从那里加载配置,包括应用程序可以用来验证令牌签名的公钥。

要么有东西阻止了连接(如防火墙),要么 Azure AD 已关闭(极不可能)。

【讨论】:

  • 谢谢 juunas 和 @Nan-yu 我想我必须通过基础设施弄清楚,是否有任何我们必须列入白名单的 URL 列表,以便防火墙允许链接/配置显示?我可以看到这个docs.microsoft.com/en-us/azure/azure-portal/… ......这是否足够?,也从我试图运行应用程序的服务器关闭了防火墙,我从“具有高级安全性的 Windows 防火墙”选项卡和域配置文件、私人配置文件中检查了它并且公共配置文件显示“Windows 防火墙已关闭”
  • 谢谢@juunas,我明天会和他们一起检查。另外,我是否需要在 .net 核心代码中包含 [link]login.microsoftonline.com/common 来解决 CORS 问题?
  • 什么CORS问题?你能再解释一下吗?
  • 我发布的异常来自 EventViewer,但是当我看到 DevTools 输出时Access to XMLHttpRequest at 'http://MyInternalAPIURL' from origin 'http:/MyClientURL' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. API 在 .net Core 3.0 中,客户端来自 Angular,所以这就是为什么我认为我们可能需要在 .WithOrigins("allowedOrigins") 中包含 microsoft url
  • juunas 和 @nan-yu 感谢您显示正确的方向。 juunas 感谢您的验证,当我们将在上述 cmets 中的 URL 中找到的站点列入白名单时,这一切都神奇地工作,也没有 CORS 问题。谢谢你。我会将其标记为答案..
猜你喜欢
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
  • 2022-11-01
  • 2017-02-13
  • 2019-03-23
  • 1970-01-01
相关资源
最近更新 更多