【发布时间】:2020-10-30 14:44:47
【问题描述】:
我有一个使用 AzureB2C 进行身份验证的 ASP.NET Core WebApplication (REST API)。这是 Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme)
.AddAzureADB2CBearer(options =>
{
options.Instance = "https://tenant.b2clogin.com/tfp/";
options.ClientId = "...";
options.Domain = "tenant.onmicrosoft.com";
options.SignUpSignInPolicyId = "B2C_1A_signup_signin";
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}
为了支持自动化测试中的身份验证,我确实尝试使用client credential Flow,但发现it is not supported in AzureB2C。接下来我遇到了ROPC Flow,建议作为替代方案(例如:here)。但我无法让它工作。
如果我使用 ROPC 流程收到令牌。
dynamic result = await $"https://{tenantName}.b2clogin.com"
.AppendPathSegment($"{tenantName}.onmicrosoft.com")
.AppendPathSegment("B2C_1_ROPC_Auth")
.AppendPathSegment("oauth2/v2.0/token")
.SetQueryParams(new
{
client_id,
scope = $"6d1aa76b-10fb-47bb-bbec-5d7f7a4e08c4 openid offline_access",
username,
password,
grant_type = "password",
repsonse_type = "token id_token"
}
)
.PostAsync(new StringContent(""))
.ReceiveJson();
令牌看起来不错且有效。但是如果我尝试使用它来验证我对 REST Api 的调用,我会收到以下错误:
WWW-认证 Bearer error="invalid_token", error_description="找不到签名密钥"
经过一番调查,看起来 RestAPI 正在尝试使用我的 B2C_1A_signup_signin 策略的众所周知的配置来验证 KID。这是有道理的,因为它是AddAzureADB2CBearer 选项的一部分。如果我将 Policy 更改为 B2C_1_ROPC_Auth,则接受令牌。
但我确实需要两个令牌才能工作,即使用B2C_1A_signup_signin Flow 和 ROPC flow 令牌创建的令牌。
如何配置我的应用以接受这两种配置?
【问题讨论】:
标签: c# asp.net-core jwt openid azure-ad-b2c