Azure AD B2C - IDX20803: 无法从 'System.String' 获取配置。 (.Net 5 Web API + Angular)

3
我有一个Angular应用程序和一个Web Api应用程序(.Net 5.0),我需要使用Azure AD B2C对用户进行身份验证。因此,当用户需要访问某些受保护的路由时,他将被重定向到B2C登录页面,进行身份验证并重定向回Angular应用程序。这个Angular部分是没有问题的。
现在,当Angular应用程序向受保护的Web API(.Net 5)发出请求时,这个Web API应该授权该请求。我以前在早期版本的.Net Core中做过这件事。但是这是新的、闪亮的Microsoft Identity web。它不起作用。
设置如下:
B2C的appsettings.json
  "AzureAdB2C": {
    "TenantId": "[Tenant Guid]",
    "Instance": "https://[tenant name].b2clogin.com/",
    "ClientId": "[web api client Id]",
    "Domain": "[tenant name].onmicrosoft.com",
    "SignInPolicyId": "B2C_1_signin"
  },

startup.cs - ConfigureServices()

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  .AddMicrosoftIdentityWebApi(options =>
  {
    Configuration.Bind("AzureAdB2C", options);

    options.TokenValidationParameters.NameClaimType = "name";
  },
    options => { Configuration.Bind("AzureAdB2C", options); });

// Creating policies that wraps the authorization requirements
services.AddAuthorization();

services.AddApplication();
services.AddInfrastructure(Configuration);

services.AddRouting(options => options.LowercaseUrls = true);
services.AddMemoryCache();

services.AddControllers().AddNewtonsoftJson();

startup.cs - Configure()

...
app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
  // endpoints.MapRazorPages();
  endpoints.MapControllers();
});

使用这个设置,我只得到了一个非常详细的错误提示:

System.InvalidOperationException: IDX20803: 无法从 'System.String' 获取配置。 ---> System.IO.IOException: IDX20807: 无法从 'System.String' 检索文档。HttpResponseMessage: 'System.Net.Http.HttpResponseMessage',HttpResponseMessage.Content: 'System.String'。 at Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(String address, CancellationToken cancel) at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.GetAsync(String address, IDocumentRetriever retriever, CancellationToken cancel) at Microsoft.IdentityModel.Protocols.ConfigurationManager1.GetConfigurationAsync(CancellationToken cancel) --- 内部异常堆栈跟踪的结尾 --- at Microsoft.IdentityModel.Protocols.ConfigurationManager1.GetConfigurationAsync(CancellationToken cancel) at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync() at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync() at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.AuthenticateAsync() at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

然后我将这个“helper”糖配置更改为以下内容:
services.AddAuthentication(options => { options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; })
  .AddJwtBearer(jwtOptions =>
  {
    jwtOptions.Authority = $"{Configuration["AzureAdB2C:Instance"]}/{Configuration["AzureAdB2C:TenantId"]}/{Configuration["AzureAdB2C:SignInPolicyId"]}/v2.0/";
    jwtOptions.Audience = Configuration["AzureAdB2C:ClientId"];
    jwtOptions.Events = new JwtBearerEvents
    {
      OnAuthenticationFailed = AuthenticationFailed
    };
});

它有效!

那么,有没有关于如何真正配置MS Identity Web的好文档?为什么异常中没有精确的描述告诉我们缺少什么?

更新

感谢回答,但是这个Identity Web实现真的很差。

这个异常的原因是SignUpSignInPolicyId未定义或拼写错误!Microsoft团队,你必须将其添加到异常中!

  1. 添加有意义的异常
  2. 如果我不想使用SignUpSignIn策略(我不需要任何陌生人来注册我的应用程序),为什么它没有被记录和支持?
  3. 使用可选日志记录,以告诉开发人员出了什么问题/丢失了什么!
  4. 使用Builder模式创建配置。然后在特定情况下更容易确定缺少哪个配置部分。

异常说明可以访问OIDC元数据端点。该URL是使用Authority构建的,Authority是使用instance、tenantId和policyId构建的。由于某些原因,看起来默认配置无法构建/检索该URL。 - undefined
这是最新的文件参考:https://learn.microsoft.com/zh-cn/azure/active-directory-b2c/enable-authentication-web-application?tabs=visual-studio#initiate-the-authentication-libraries - undefined
1
  1. 一个例子 - 这样行不通,首先是services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAdB2C");
  2. "异常说明您可以访问OIDC元数据终结点" - 真的吗?在异常文本中给我看看。
  3. 为什么不能告诉我缺少哪个部分?
- undefined
我能理解你对Microsoft.Identity.Web的痛苦,文档和支持实在是太差了,尤其是针对.Net 5.0。 - undefined
同时正在处理这个问题,但是 .Net 6.0 没有改进。仍然出现同样的错误。 :-( - undefined
2个回答

3

我不需要注册/登录政策 - 我不希望任何用户去注册我的应用程序!我只需要登录政策。 - undefined
这不是重点,关键的名字很重要。它用于构建OIDC元数据URL,这并不意味着你需要或者会拥有注册功能。 - undefined
为什么一开始就需要注册/登录政策?这是API,不是客户端。 - undefined
这是因为它是AAD B2C中权威的一部分,所以必须有一个policyId来构建OIDC元数据URL,该URL托管令牌签名密钥。SUSI是最常见的,因此是密钥名称。 - undefined
@JasSuri-MSFT 这是指用于首次发行令牌的政策吗?所以实际上没有进行身份验证,这纯粹是为了验证令牌本身? - undefined
是的,就是这样。密钥名称有误导性,你只需要一个policyId来验证令牌,因为这是获取签名密钥所必需的。 - undefined

0

原来如此

 "AzureAdB2C": {
    "Instance": "",
    "Domain": "",
    "TenantId": "",
    "ClientId": "",
    "Authority": "",
    "CallbackPath": "/signin-oidc"

缺少权限。文档质量真的很差!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接