我遵循这个正式的MS 文档,使用Azure B2C为两个安全Web (比如Web 1和2)实现OBO流。前面的链接指向Git上的以下示例。
基本上,我使用的代码是相同的:
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://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并到达经过测试的端点时,代码行如下:
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通信的权限。
对于给定的错误,我一直在做一些研究/调查,并尝试了一些不同的方法,但没有运气。
有谁知道如何解决这个问题吗?
提前感谢
发布于 2021-08-22 15:44:53
如果这些确实是通过Azure AD B2C端点发出的令牌,那么它就是不兼容的。
但是,您的错误代码显示您正在尝试使用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
您不能将这两个端点放在一起用于不同的事情。
https://stackoverflow.com/questions/68868643
复制相似问题