【发布时间】:2021-03-01 22:46:06
【问题描述】:
我们正在开发一种 API,允许用户通过多个不同的提供商进行身份验证。单独的提供者不是问题,但将它们一起使用被证明是一个挑战。
似乎添加超过 1 个提供程序会在应用程序启动时引发带有“方案已存在:承载”的 InvalidOperationException。
下面是来自Startup.cs的ConfigureServices函数
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();
});
}
我使用 Microsoft 示例 for authenticating with Azure AD 作为起点。删除 AddJwtBearer 或 AddMicrosoftIdentityWebApi 调用工作正常,但我需要为我们的用例配置这两个提供程序。
.NET Core 3.1 或更高版本有没有办法做到这一点?
【问题讨论】:
-
这个错误是有道理的,看起来内部方案
Bearer被两个提供商使用。我认为他们可能有一些选择来改变方案,如果没有的话,你需要编写自己的提供者来结合这两个提供者。这就是解决这个问题的逻辑,但实际上我以前从未做过类似的事情。 -
您找到解决方法了吗?我也在寻找解决方案
-
不,还没有。对于我们的解决方案,我们已经做出让步,即在应用程序的生命周期内只使用其中一种,因为我们通常不会在任何生产场景中混合和匹配。也就是说,可能可以剖析
AddJwtBearer调用并指定不同的方案,以免它与AddMicrosoftIdentityWebApi调用中使用的Bearer方案发生冲突。如果我有空闲时间,我会尝试举一个例子作为答案:)
标签: c# asp.net-core authentication jwt azure-ad-b2c