Azure B2C 身份验证(angular + .net core Web API)-在不记名令牌中找不到范围或角色声明

时间:2021-04-08 09:32:41

标签: c# angular azure asp.net-core azure-ad-b2c

我尝试使用 MSAL-angular 和 Azure B2C 进行 angular 应用程序进行身份验证。 我能够使用 Azure B2C 对 Angular 应用程序进行身份验证(我已经创建了一个 susi 流)并获得了如下图所示的令牌

Token aquisition

所以我创建了一个 .net core web api 项目并修改了 appsetting 配置并使用此代码启动:

appsetting.json :

    "AzureAdB2C": {
    "Instance": "https://{mytenat}.b2clogin.com/tfp",
    "ClientId": "8xxxx-xxxx-xxxx-xxxx-xxxxxxxxc",
    "Domain": "{mytenat}.onmicrosoft.com",
    "SignUpSignInPolicyId": "B2C_1_susi"
  }

startup.cs

            JwtSecurityTokenHandler.DefaultMapInboundClaims = false;

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

            // By default, the claims mapping will map claim names in the old format to accommodate older SAML applications.
            //'http://schemas.microsodt.com/ws/2008/06/identity/clains/role' instead of 'roles'
            // This flag ensures that the ClaimsIdentity claims collection will be build from the claims in the token
            JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
            //services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
            //{
            //    // The claim in the Jwt token where App roles are available.
            //    options.TokenValidationParameters.RoleClaimType = "roles";
            //});

但是如果我尝试在本地运行项目并使用邮递员调用它,我会遇到此错误:

**System.UnauthorizedAccessException:IDW10201:在不记名令牌中找不到范围或角色声明。 **

我不明白错误在哪里。 你能帮我吗?

谢谢

1 个答案:

答案 0 :(得分:0)

在您的问题中,您已在 Azure AD B2C 中创建了一个 web api 应用程序和 angular 应用程序。接下来需要公开web api应用的api,然后给angular应用添加权限。

首先进入web api。

enter image description here

然后转到 angular 应用>API 权限> 添加权限>我的 API>您的 Web api 应用。

enter image description here

最后,当您解析令牌时,您将看到 scp:access 声明。

相关问题