Identityserver 4 和 Ocelot

tri*_*tri 5 c# .net-core identityserver4 api-gateway ocelot

我正在尝试按照https://ocelot.readthedocs.io/en/latest/features/authentication.html使用 Ocelot 和 IS4

使用时

public void ConfigureServices(IServiceCollection services)
{
    var authenticationProviderKey = "TestKey";

    services.AddAuthentication()
        .AddJwtBearer(authenticationProviderKey, x =>
        {
        });
}
Run Code Online (Sandbox Code Playgroud)

并在 ocelot.json 中使用“TestKey”,它在启动应用程序时抛出错误

无法启动 Ocelot,错误是:TestKey,AllowedScopes:[] is unsupported authentication provider

知道出了什么问题吗?我需要在我的 IdentityServer 应用程序中特别设置一些东西吗?

小智 3

您需要添加选项,例如:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        // base-address of your identityserver
        options.Authority = "https://demo.identityserver.io";

        // name of the API resource
        options.Audience = "api1";
    });
Run Code Online (Sandbox Code Playgroud)

更多信息请访问:http://docs.identityserver.io/en/latest/topics/apis.html#

您还需要将 API 资源添加到您的 Identity Server:

new ApiResource("api1", "Some API 1")
Run Code Online (Sandbox Code Playgroud)

看:

http://docs.identityserver.io/en/latest/topics/resources.htmlhttp://docs.identityserver.io/en/latest/reference/api_resource.html#refapiresource