【问题标题】:Getting error AADST50013 for OBO flow using Azure AD B2C on .Net Core 3.1在 .Net Core 3.1 上使用 Azure AD B2C 获取 OBO 流的错误 AADST50013
【发布时间】:2021-08-20 22:32:02
【问题描述】:

我正在关注这个官方 MS doc,使用 Azure AD B2C 为两个安全 Web API(比如说 Web API 1 和 2)实现 OBO 流程。之前的链接指向 Git 上的以下 example

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

MyController.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://docs.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 与 Web API 2 通信的权限。

对于给定的错误,我一直在做一些研究/调查,并尝试了几种不同的方法,但没有运气。

有人知道如何解决这个问题吗?

提前致谢

【问题讨论】:

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


    【解决方案1】:

    如果这些确实是通过 Azure AD B2C 端点发布的令牌,那么它根本不兼容。

    https://docs.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 流。 IE。用户令牌从 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

    您不能将这两个端点一起用于不同的事情。

    【讨论】:

    • 感谢您分享此信息。现在,我对它有了更好的理解。但是,仍在试图弄清楚如何实现它。您是否知道或指出有关如何在两个安全 Web api 之间进行通信的文档?我正在使用客户端凭据流进行测试,但收到 401 未经授权的错误
    猜你喜欢
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2017-03-19
    • 2020-07-14
    • 1970-01-01
    • 2020-10-09
    相关资源
    最近更新 更多