首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在OBO核心3.1上使用Azure AD B2C获取OBO流的误差B2C

在OBO核心3.1上使用Azure AD B2C获取OBO流的误差B2C
EN

Stack Overflow用户
提问于 2021-08-20 22:32:02
回答 1查看 554关注 0票数 0

我遵循这个正式的MS 文档,使用Azure B2C为两个安全Web (比如Web 1和2)实现OBO流。前面的链接指向Git上的以下示例

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

MyController.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddMicrosoftIdentityWebApi(options =>
            {
                Configuration.Bind("AzureAdB2C", options);
                options.TokenValidationParameters = new TokenValidationParameters { SaveSigninToken = true };
            }, options => { Configuration.Bind("AzureAdB2C", options); });

当我使用Swagger运行Web并到达经过测试的端点时,代码行如下:

代码语言:javascript
复制
AuthenticationResult result = await app.AcquireTokenOnBehalfOf(scopes, userAssertion).ExecuteAsync();

引发以下错误:

AADSTS50013:断言签名验证失败。原因-钥匙没找到。跟踪ID: c0d53284-12f3-4ab0-a42c-d7c35e2ad300相关ID: e37849e8-938b-441e-ID 80-d1612733dc17时间戳: 2021-08-20 22:13:53Z

从Azure B2C,我已经从应用程序注册授予Web 1与Web 2通信的权限。

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

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

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2021-08-22 15:44:53

如果这些确实是通过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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68868643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档