小编Nin*_*ark的帖子

对来自多个来源(例如 Cognito 和 Azure)的令牌进行身份验证

我们正在开发一种 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 进行身份验证作为起点。删除AddJwtBearerorAddMicrosoftIdentityWebApi调用工作正常,但我需要为我们的用例配置两个提供程序。

有没有办法在 .NET Core 3.1 或更高版本上做到这一点?

c# authentication jwt azure-ad-b2c asp.net-core

7
推荐指数
1
解决办法
398
查看次数

标签 统计

asp.net-core ×1

authentication ×1

azure-ad-b2c ×1

c# ×1

jwt ×1