簡體   English   中英

Azure B2C Authentication (angular + .net core Web API) - Neither scope or roles claim was found in the bearer token

[英]Azure B2C Authentication (angular + .net core Web API) - Neither scope or roles claim was found in the bearer token

我嘗試使用 MSAL-angular 和 Azure B2C 進行 angular 應用程序進行身份驗證。 我能夠使用 Azure B2C 驗證 angular 應用程序(我創建了一個 susi 流)並獲得令牌,如下圖所示

代幣獲取

所以我創建了一個 .net 核心 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"
  }

啟動.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";
            //});

但是如果我嘗試在本地運行項目並使用 postman 調用它,我會遇到此錯誤:

**System.UnauthorizedAccessException:IDW10201:在不記名令牌中找不到 scope 或角色聲明。 **

我不明白錯誤在哪里。 你能幫助我嗎?

謝謝

在您的問題中,您在 Z3A580F1422203677F13FZ0BC3 中創建了 web api 應用程序和 angular 應用程序接下來,您需要公開web api應用程序的api,然后將權限添加到ZD18B8624A0F5F5F5621應用程序。

首先是 go 到 web api。

在此處輸入圖像描述

然后 go 到 angular 應用程序>API 權限>添加權限>我的 APIs>你的 web Z8A5DA52ED126427DZ 應用程序。

在此處輸入圖像描述

最后,當您解析令牌時,您將看到scp:access聲明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM