如何在 ASP.NET Core 3.1 产品中配置 IdentityServer

How to configure IdentityServer in ASP.NET Core 3.1 production

我正在尝试 运行 一个 ASP.NET Core 3.1 应用程序,它在开发中运行良好,但在生产环境中,我收到以下错误。

An error occurred while starting the application.
InvalidOperationException: Key type not specified.

Microsoft.AspNetCore.ApiAuthorization.IdentityServer.ConfigureSigningCredentials.LoadKey()

InvalidOperationException: Key type not specified. Microsoft.AspNetCore.ApiAuthorization.IdentityServer.ConfigureSigningCredentials.LoadKey() Microsoft.AspNetCore.ApiAuthorization.IdentityServer.ConfigureSigningCredentials.Configure(ApiAuthorizationOptions options) Microsoft.Extensions.Options.OptionsFactory.Create(string name) ...

我的appsettings.json包含

"IdentityServer": {
    "Clients": {
      "TestProjct": {
        "Profile": "IdentityServerSPA"
      }
    }
  },

appsettings.Development.json包含

"IdentityServer": {
    "Key": {
      "Type": "Development"
    }
  }

我想在生产环境中为 运行ning 配置密钥,但我找不到说明配置密钥的不同方式的文档。

我发现 an example 显示使用 .pfx 文件和证书存储,但我想查看所有可用的内容。有谁知道我在哪里可以找到这方面的文档?或者可以提供一些额外的示例来说明如何为生产配置密钥?

由于 asp.net 核心 3.1 尚不支持从 .pem 文件导入密钥,因此像您的示例 link 这样的 .pfx 文件是一种足够公平的方法。我强烈建议您自己生成一个,openSSL 是一种广泛使用的工具。

在屏幕后面,身份服务器允许您在 .AddSigningCredential() 中传递安全密钥,这正是 IdentityServer 对您大喊大叫的事情。

所以另一种方法是生成一个 RSAECDSA 密钥来传递它们。类似于:

// Generate the key programetically
using var keyProvider = new RSACryptoServiceProvider();
keyProvider.KeySize = 2048; // the default was 1024
var xmlKey = keyProvider.ToXmlString(true); // store the key as string somewhere

// Create the key and use it for jwt signing
var rsa = RSA.Create();
rsa.FromXmlString(xmlKey);

// On your startup
services.AddIdentityServer()
    ...
    .AddSigningCredential(new RsaSecurityKey(rsa))
    ...;

您显然可以使用其他方法生成 pfx 文件。但我个人更喜欢它而不是传递这样的密钥。如果您选择文件方法,至少可以使用广泛接受的工具生成密钥。