繁体   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