.net core 使用IdentityServer4保护多个API

.net core 使用IdentityServer4保护多个API,.net-core,identityserver4,.net Core,Identityserver4,我们使用IdentityServer4来保护我们的API,实际上我们有多个API,我们希望使用IdentityServer4(即通过生成访问令牌)来保护这些API,但是我们对验证访问令牌有疑问,我们是否需要在每个API中编写以下代码 public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvcCore()

我们使用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();
    }
}

实际上,我们正在学习本教程(“”),他们提到我们必须在相应的API中对此进行描述。

您应该创建一个单独的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();
}

你的问题的简短答案是-是的,你知道

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

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

从我看到的情况来看,您已经有了.NET核心方法的代码,而且看起来不错

可以找到用于.NET Framework API的


希望这能有所帮助。

Hi@Metehan,如果我们必须像您上面提到的那样创建单独的身份验证服务器,那么我们是否需要创建与每个api对应的多个身份验证服务器?因为如果我们提到option.Audience=“api1”;然后它将只授权我们的“api1”应用。嗨@Sonika,不,你不会创建多个身份验证服务器。这段代码用于访问令牌验证,它应该在您想要保护的api中。“api1”应该是您的api名称。您的api名称应在identity server的identity resources中配置。但我们不希望在api项目中编写任何代码,因为这些api已经生成和部署。您必须在api项目中添加“IdentityServer4.AccessTokenValidation”nuget包和AddAuthentication和UseAuthentication部分,如上文所述。是的。我想这就是重点。您可以配置每个API如何与标识服务器“对话”。
 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();
}