【问题标题】:Protect multiple APIs with IdentityServer4使用 IdentityServer4 保护多个 API
【发布时间】:2018-02-27 06:51:38
【问题描述】:

我们使用 IdentityServer4 来保护我们的 API,实际上我们有多个 API,我们想用 IdentityServer4 保护这些 API(即通过生成访问令牌)但是我们对验证访问令牌有疑问,我们是否需要编写以下代码在每个 API 中?

  public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvcCore()
            .AddAuthorization()
            .AddJsonFormatters();

        services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "http://localhost:5000";
                options.RequireHttpsMetadata = false;

                options.ApiName = "api1";
            });
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseAuthentication();

        app.UseMvc();
    }
}

实际上我们正在关注本教程(“http://docs.identityserver.io/en/release/quickstarts/1_client_credentials.html”),他们提到我们必须在相应的 API 中描述这一点。

【问题讨论】:

  • 是的。我认为这就是重点。您可以配置每个 API 如何“与”身份服务器“对话”。

标签: .net-core identityserver4


【解决方案1】:

您的问题的简短回答是 - 是的,您愿意

解释 - 你必须告诉每个 API 使用什么身份验证,以及哪个是提供者。

根据平台(.NET Framework 或 Core),您应该使用 IdentityServer3.Contrib.AccessTokenValidation(冻结分支的最新 fork)或 IdentityServer4.AccessTokenValidation 包。

据我所知 - 您已经有了 .NET Core 方法的代码,而且看起来不错。

.NET Framework API 的一个可以找到here

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您应该创建一个单独的 AuthServer 应用并在其中编写这些代码。

    然后您可以使用 IdentityServer4.AccessTokenValidation 包验证您的 AuthServer 生成的 jwt 令牌。

    你的 AuthServer 应该是这样的;

    public void ConfigureServices(IServiceCollection services)
    {
        var idSrvBuilder = services.AddIdentityServer()
            .AddSigningCredential(new X509Certificate2(
                Path.Combine(Environment.ContentRootPath, "certs", "yourcert.pfx"), "yourcertpass",
                X509KeyStorageFlags.MachineKeySet))
            .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>()
            .AddProfileService<ProfileService>()
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients());
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseIdentityServer();
    }
    

    IdentityServer 配置可以看这里:http://docs.identityserver.io/en/release/topics/startup.html

    在您的其他 api 中,您可以验证 jwt 令牌,如下所示;

     public void ConfigureServices(IServiceCollection services)
     {
        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                    .AddJwtBearer(options =>
                    {
                        options.Authority = "http://YourAuthServerUrl";
                        options.RequireHttpsMetadata = false;
                        options.Audience = "api1";
                    });
     }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        builder.UseAuthentication();
    }
    

    【讨论】:

    • 嗨@Metehan,如果我们必须像您上面提到的那样创建单独的身份验证服务器,那么我们是否需要为每个api创建多个身份验证服务器?
    • 因为如果我们提到 option.Audience = "api1";那么它只会授权我们的“api1”应用程序。
    • 嗨@Sonika,不,您不会创建多个身份验证服务器。此代码用于访问令牌验证,它应该在您要保护的 api 中。 “api1”应该是您的 api 名称。您的 api 名称应该在身份服务器的身份资源中配置。
    • 但是我们不想在我们的 api 项目中编写任何代码,因为那些 api 已经构建和部署了
    • 您必须在您的 api 项目中添加“IdentityServer4.AccessTokenValidation” nuget 包和 AddAuthentication 和 UseAuthentication 部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 2018-08-20
    • 2021-06-20
    • 1970-01-01
    相关资源
    最近更新 更多