簡體   English   中英

使用 Azure AD B2C on.Net Core 3.1 獲取 OBO 流的錯誤 AADST50013

[英]Getting error AADST50013 for OBO flow using Azure AD B2C on .Net Core 3.1

我正在按照此官方 MS 文檔使用 Azure AD B2C 為兩個安全的 Web API(假設為 Web API 1 和 2)實施 OBO 流程。 之前的鏈接指向 Git 上的以下示例

基本上,我使用相同的代碼:

我的控制器.cs

string[] scopes = { "profile.read.basic", "user.read" };
UserProfile profile = null;
        
try
{
   string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenantId);
            
   ClaimsPrincipal principal = HttpContext.User as ClaimsPrincipal;
   //Grab the Bearer token from the HTTP Header using the identity bootstrap context. This requires SaveSigninToken to be true at Startup.Auth.cs
   var bootstrapContext = principal.Identities.First().BootstrapContext?.ToString();

   // Creating a UserAssertion based on the Bearer token sent by TodoListClient request.
   //urn:ietf:params:oauth:grant-type:jwt-bearer is the grant_type required when using On Behalf Of flow: https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-on-behalf-of-flow
   UserAssertion userAssertion = new UserAssertion(bootstrapContext, "urn:ietf:params:oauth:grant-type:jwt-bearer");
            
   // Creating a ConfidentialClientApplication using the Build pattern (https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/Client-Applications)
   var app = ConfidentialClientApplicationBuilder.Create(clientId)
            .WithAuthority(authority)
            .WithClientSecret(appKey)
            .WithRedirectUri(redirectUri)
            .Build();

   // Acquiring an AuthenticationResult for the scope user.read, impersonating the user represented by userAssertion, using the OBO flow
   AuthenticationResult result = await app.AcquireTokenOnBehalfOf(scopes, userAssertion).ExecuteAsync();

在 StartUp.cs 上,我必須將 SaveSigninToken 設置為 true

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

當我使用 Swagger 運行 Web API 並點擊以下代碼行測試端點時:

AuthenticationResult result = await app.AcquireTokenOnBehalfOf(scopes, userAssertion).ExecuteAsync();

拋出以下錯誤:

AADSTS50013:斷言簽名驗證失敗。 [原因 - 未找到密鑰。] 跟蹤 ID:c0d53284-12f3-4ab0-a42c-d7c35e2ad300 關聯 ID:e37849e8-938b-441e-bd80-d1612733dc17 時間戳:2021-08-20 22:13:53Z

從 Azure AD B2C 我已經授權 Web API 1 從 App Registration 與 Web API 2.

對於給定的錯誤,我一直在做一些研究/調查,並嘗試了幾種不同的方法,但沒有成功。

有誰知道如何解決這個問題?

提前致謝

如果這些真的是通過 Azure AD B2C 端點發行的令牌,那么它就是不兼容的。

https://learn.microsoft.com/en-us/azure/active-directory-b2c/custom-policy-developer-notes#oauth-20-application-authorization-flows

但是,您的錯誤代碼顯示您正在嘗試使用 Azure AD 端點,可能是您的 Azure AD B2C 租戶。

在這種情況下,您似乎正在使用 Azure AD B2C 簽名令牌,並嘗試針對 Azure AD 端點執行 OBO,並且兩者都使用不同的簽名密鑰。 因此,這是行不通的。

只有 Azure AD 端點會執行 OBO 流。 即從 Azure AD 令牌端點發出的用戶令牌,並使用用戶 AAD 令牌對同一端點執行 OBO。

AAD 終結點: https://login.microsoftonline.com/<tenant>/oauth2/v2.0/token

AAD B2C 終結點: https://contoso.b2clogin.com/<tenant>/<B2C-policy-id>/oauth2/v2.0/token

您不能將這兩個端點一起用於不同的事情。

暫無
暫無

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

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