【发布时间】: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