我们正在开发一种 API,它允许用户通过许多不同的提供商进行身份验证。单独的提供程序不是问题,但事实证明将它们一起使用是一个挑战。
InvalidOperationException
当应用程序启动时,似乎添加 1 个以上的提供者会抛出“方案已经存在:承载”。
下面是ConfigureServices
函数来自Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "Value";
options.Audience = "Value";
})
.AddMicrosoftIdentityWebApi(options =>
{
Configuration.Bind("AzureAdB2C", options);
options.TokenValidationParameters.NameClaimType = "name";
},
options => { Configuration.Bind("AzureAdB2C", options); });
services.AddControllers();
services.AddAuthorization(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder(
JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser()
.Build();
});
}
Run Code Online (Sandbox Code Playgroud)
我使用 Microsoft 示例对 Azure AD 进行身份验证作为起点。删除AddJwtBearer
orAddMicrosoftIdentityWebApi
调用工作正常,但我需要为我们的用例配置两个提供程序。
有没有办法在 .NET Core 3.1 或更高版本上做到这一点?