【问题标题】:Azure AD B2C Authenticating in backend web API built with dotnet core 5.0Azure AD B2C 在使用 dotnet core 5.0 构建的后端 Web API 中进行身份验证
【发布时间】:2021-10-12 17:23:08
【问题描述】:

我尝试在 dotnet 5.0 微服务 webapi 中对 Azure AD B2C 应用进行身份验证失败。

我参考了在以下位置托管的项目的教程:https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2.git

在 Startup.cs 文件的 ConfigureServices 下,我根据示例项目尝试了以下操作:

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

我收到以下错误消息:

Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler: Error: Exception occurred while processing message.

System.InvalidOperationException: IDX20803: Unable to obtain configuration from: 'https://xxxxxx.b2clogin.com/Tenant-xxx-xxx-xxx-xxxxxxID/v2.0/.well-known/openid-configuration'
---> System.IO.IOException: IDX20807: Unable to retrieve document from: 'https://xxxxxx.b2clogin.com/Tenant-xxx-xxx-xxx-xxxxxxID/v2.0/.well-known/openid-configuration'. HttpResponseMessage: 'StatusCode: 404, ReasonPhrase: 'Not Found', ...

我还尝试了以下方法:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
          .AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAdB2C"));

以上这个认证方式返回登录模板html页面。

参考未在 appsettings.json 中包含 TenantId 的项目,我收到错误消息,抱怨缺少 TenantId。所以现在在 appsettings.json 中,我有以下详细信息:

"AzureAdB2C": {
    "Instance": "https://xxxxxx.b2clogin.com/",
    "ClientId": "xxxx-xxx-xxx-xx-xxxx",
    "Domain": "xxxxxx.onmicrosoft.com/",
    "SigninPolicyId":"B2C_1_testsusi",
    "TenantId": "Tenant-xxx-xxx-xxx-xxxxxxID"
  }

非常感谢任何帮助!

【问题讨论】:

    标签: azure .net-core oauth-2.0 azure-ad-b2c


    【解决方案1】:

    由于 appsettings.json 中的键名不正确,该 openId 元数据 URL 中缺少 PolicyId。

    在 appsettings.json 中将密钥 SigninPolicyId 更改为 SignUpSignInPolicyId

    https://docs.microsoft.com/en-us/azure/active-directory-b2c/enable-authentication-web-application?tabs=visual-studio#add-the-app-settings

    【讨论】:

      猜你喜欢
      • 2019-11-19
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 2017-04-08
      相关资源
      最近更新 更多