0

问题:我作为应用程序调用下游 Web api 在添加我自己的 jwt 不记名身份验证后引发空异常错误。

我有一个 .net 5 Web API,称之为 AppAPI,其 ConfigureServices 具有以下代码:

var accessTokenKey = Convert.FromBase64String(Configuration.GetValue<string>("AccessCodeSecret"));

        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
               .AddJwtBearer("AccessToken", o =>
               {
                   o.RequireHttpsMetadata = false;
                   o.SaveToken = true;
                   o.TokenValidationParameters = new TokenValidationParameters
                   {
                       ValidateIssuerSigningKey = true,
                       IssuerSigningKey = new SymmetricSecurityKey(accessTokenKey),
                       ValidateIssuer = false,
                       ValidateAudience = false
                   };
               })
              .AddMicrosoftIdentityWebApi(Configuration, "AzureAd", "AzureAd")
              .EnableTokenAcquisitionToCallDownstreamApi()
              .AddDownstreamWebApi("CommonServicesApi", Configuration.GetSection("CommonServicesApi"))
              .AddInMemoryTokenCaches();

        //services.AddAuthorization();
        services.AddAuthorization(options =>
        {
            options.DefaultPolicy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .AddAuthenticationSchemes("AccessToken")
                .Build();
        });

我有一个 /auth 端点,它接受来自 Azure AD 的访问令牌,并基于数据库使用我自己的自定义声明生成一个新的访问令牌。控制器使用授权属性来确保它使用正确的机制:

[Authorize(AuthenticationSchemes = "AzureAd")]

上面的默认策略确保其余端点对未指定方案的每个其他端点使用此访问令牌。

我有第二个 Web API,称为 CommonServices,只能从其他 API 访问,不能直接从客户端访问。所以 AppAPI 使用 AddDownstreamwebapi 来处理这些调用。这对我之前添加我自己的应用程序访问令牌有效,这意味着我只有一种身份验证机制 - AddMicrosoftIdentityWebApi。当我添加自己的 JwtBearer 身份验证 - “AccessToken”时,我开始收到错误消息。

有错误的控制器注入 IDownstreamWebApi commonServicesApi。它使用“AccessToken”的默认身份验证方案。代码如下所示:

 var response = await _commonServicesApi.CallWebApiForAppAsync("CommonServicesApi", "AzureAd",
             options => { options.RelativePath = "Projects"});
            var json = await response.Content.ReadAsStringAsync();

第二个参数“AzureAd”是我尝试让 commonservicesApi 使用正确的方案。我什至不确定这是否是正确的方案,或者 .EnbleTokenAcquisitionToCallDownstreamApi 是否添加了应该指定的第三种方案。

我接到的就是这个电话

System.NullReferenceException: 'Object reference not set to an instance of an object.'
at Microsoft.Identity.Web.MergedOptions.PrepareAuthorityInstanceForMsal()
This exception was originally thrown at this call stack:
Microsoft.Identity.Web.MergedOptions.PrepareAuthorityInstanceForMsal()
Microsoft.Identity.Web.TokenAcquisition.BuildConfidentialClientApplication(Microsoft.Identity.Web.MergedOptions)
Microsoft.Identity.Web.TokenAcquisition.GetOrBuildConfidentialClientApplication(Microsoft.Identity.Web.MergedOptions)
Microsoft.Identity.Web.TokenAcquisition.GetAuthenticationResultForAppAsync(string, string, string, Microsoft.Identity.Web.TokenAcquisitionOptions)
Microsoft.Identity.Web.DownstreamWebApi.CallWebApiForAppAsync(string, string, System.Action<Microsoft.Identity.Web.DownstreamWebApiOptions>, System.Net.Http.StringContent)
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

我似乎无法弄清楚什么是空的,或者如何解决这个问题。

4

0 回答 0