在 Linux 应用服务上为 IdentityServer4 提供密钥

Providing IdentityServer4 with a key on Linux App Service

我正在尝试在 Linux 应用服务 (B1) 上的 netcore 3.1 应用程序 运行 上配置 IdentityServer4。我正在尝试从文件系统加载密钥并在配置时将其传递给 IdentityServer (as per here), but the key I provide seems to be getting thrown away as IdentityServer tries to look for config to find key itself (line 70 appears to be where the exception is thrown from)。

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.AddIdentityServer()
            .AddSigningCredential(GetCert())
            .AddApiAuthorization<IdentityUser, ApplicationDbContext>();

        services.AddAuthentication()
            .AddIdentityServerJwt();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseAuthentication();
        app.UseIdentityServer(); // exception thrown here 
        app.UseAuthorization();
    }


    private X509Certificate2 GetCert()
    {
        // Linux App Service puts keys configured in the App Service in this directory
        // I have checked - the key is correctly placed here
        var bytes = System.IO.File.ReadAllBytes("/var/ssl/private/XYZTHUMBPRINTXYZ.p12");
        return new X509Certificate2(bytes);
    }

我应该注意,这实际上不是将密钥放到 Azure 或应用程序本身上的问题,我已经在文件系统上验证了它,应用程序可以从中创建一个有效的密钥,看起来被 IdentityServer 丢弃。

因为 Window 的类型 KeyStore 在 Linux 服务上不可用,appsettings 配置(下面的示例)不起作用。

  "IdentityServer": {
    "Key": {
      "Type": "Store",
      "StoreName": "My",
      "StoreLocation": "CurrentUser",
      "Name": "CN=example.com"
    }   
  },

在回答这个问题的帮助下,我已经弄清楚了 -

问题是 AddApiAuthorization<IdentityUser, ApplicationDbContext>() 在内部调用 AddSigningCredentials() 这将指示 IdentityServer 查看应用程序配置以找出要使用的键。无论 AddSigningCredential(cert) 是在 AddSigningCredential(cert) 之前还是之后调用,它都会执行此操作。

为了解决我的问题,我内联了 AddApiAuthorization<IdentityUser, ApplicationDbContext>() 方法并从配置中删除了查找更多键的行,应用程序现在可以在 linux appservice 上运行。

我还在 IdentityServer 的 github 上提出了问题,留下 link 以防项目所有者对决议发表评论 - https://github.com/IdentityServer/IdentityServer4/issues/4000