.Net 5:无法启动 Ocelot,不支持的身份验证提供程序

.Net 5: Unable to start Ocelot, unsupported authentication provider

我想在 Ocelot API 网关中实现 JWT 身份验证,我仔细关注了 ocelot documentation 并实现了它。但是我得到了一个错误,不知道如何解决。

我使用文档中的这个 section 来启用身份验证。

我收到的错误:

System.AggregateException: 'One or more errors occurred. (Unable to start Ocelot, errors are: Authentication Options AuthenticationProviderKey:BaseAuthenticationSchema,AllowedScopes:[] is unsupported authentication provider)'

已用包:

豹猫(17.0.0)

Microsoft.AspNetCore.Authentication.JwtBearer(5.0.11)

还有我的代码部分以获得更多规范:

Program.cs:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }
    public static IHostBuilder CreateHostBuilder(string[] args) =>
         Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
                    {
                        config
                            .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                            .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
                            .AddJsonFile($"ocelot.json", optional: false, reloadOnChange: true)
                            .AddEnvironmentVariables();
                    })
                    .ConfigureServices(s =>
                    {
                        s.AddOcelot();
                    })
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>()
                                  .UseSerilog((_, config) =>
                                  {
                                      config
                                          .MinimumLevel.Information()
                                          .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                                          .Enrich.FromLogContext()
                                          .WriteTo.File(@"Logs\AllHttpRequestsLog.txt", rollingInterval: RollingInterval.Day);
                                  })
                                  .Configure(app =>
                                  {
                                      app.UseMiddleware<HttpRequestsLoggingMiddleware>();
                                      app.UseOcelot().Wait();
                                  });
                    });
}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    // Adding Authentication
    var baseAuthenticationProviderKey = "BaseAuthenticationSchema";

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    })

    // Adding Jwt Bearer  
    .AddJwtBearer(baseAuthenticationProviderKey, options =>
    {
        options.SaveToken = true;
        options.RequireHttpsMetadata = false;
        options.TokenValidationParameters = new TokenValidationParameters()
        {
            ValidateIssuer = true,
            ValidateAudience = true,
            ValidateIssuerSigningKey = true,
            ValidateLifetime = true,
            ValidAudience = "ValidAudience",
            ValidIssuer = "ValidIssuer ",
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("IssuerSigningKey"))
        };
    });

    services.AddControllers();

    services.AddOcelot(_configuration);
}

最后使用了豹猫的配置:

{
  "DownstreamPathTemplate": "/api/v1/banks",
  "DownstreamScheme": "https",
  "DownstreamHostAndPorts": [
    {
      "Host": "localhost",
      "Port": 44371
    }
  ],
  "UpstreamPathTemplate": "/api/market/banks",
  "UpstreamHttpMethod": [ "Get" ],
  "AuthenticationOptions": {
    "AuthenticationProviderKey": "BaseAuthenticationSchema",
    "AllowedScopes": []
  }
}

我调查了所有文章,也调查了ocelot GitHub 页面这样打开issue,但我的问题并没有解决。谁能帮帮我?

非常感谢。

最后,我在 Ocelot GitHub 页面打开问题上使用这个 comment 解决了我的问题。

刚刚将身份验证配置从 startup.cs 文件移动到 program.cs 文件的 .ConfigureServices 部分。

像这样:

                    .ConfigureServices(s =>
                    {
                        // Adding Authentication
                        var baseAuthenticationProviderKey = "BaseAuthenticationSchema";

                        s.AddAuthentication(options =>
                        {
                            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                            options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
                        })

                        // Adding Jwt Bearer  
                        .AddJwtBearer(baseAuthenticationProviderKey, options =>
                        {
                            options.SaveToken = true;
                            options.RequireHttpsMetadata = false;
                            options.TokenValidationParameters = new TokenValidationParameters()
                            {
                                ValidateIssuer = true,
                                ValidateAudience = true,
                                ValidateIssuerSigningKey = true,
                                ValidateLifetime = true,
                                ValidAudience = "ValidAudience",
                                ValidIssuer = "ValidIssuer",
                                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Secret"))
                            };
                        });
                        s.AddOcelot();
                    })

此外,从 startup.cs class 中删除了该配置。