我们将 IdentityServer4 与 .NET Core Web 应用程序(“http://docs.identityserver.io/en/release/quickstarts/0_overview.html”)一起使用。我们已替换 AddDeveloperSigningCredential
与 AddSigningCredential(CreateSigningCredential())
.因为我们不能使用 AddDeveloperSigningCredential
对于生产环境,因为在生产环境中需要用一些持久的 key Material 替换。我们是 IdentityServer4 的新手,我们的问题是,以下方法可以在生产环境中创建签名凭据吗?还是我们需要在这方面做出一些改变?
这是我们的 startup.cs 文件:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfiguration>(Configuration);
//connection string
string connectionString = Configuration.GetConnectionString("IdentityServer");
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
services.AddIdentityServer().AddDeveloperSigningCredential
.AddSigningCredential(CreateSigningCredential())
// this adds the config data from DB (clients, resources)
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
}) // this adds the operational data from DB (codes, tokens, consents)
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
// this enables automatic token cleanup. this is optional.
options.EnableTokenCleanup = true;
options.TokenCleanupInterval = 30;
});
}
private SigningCredentials CreateSigningCredential()
{
var credentials = new SigningCredentials(GetSecurityKey(), SecurityAlgorithms.RsaSha256Signature);
return credentials;
}
private RSACryptoServiceProvider GetRSACryptoServiceProvider()
{
return new RSACryptoServiceProvider(2048);
}
private SecurityKey GetSecurityKey()
{
return new RsaSecurityKey(GetRSACryptoServiceProvider());
}
最佳答案
这是 gist这应该对带有 asp.net core 2.x 的 Ids4 有所帮助。
它包含一个 RsaKeyService
可以注入(inject)服务提供者的类,例如:
var rsa = new RsaKeyService(Environment, TimeSpan.FromDays(30));
services.AddTransient<RsaKeyService>(provider => rsa);
这可以确保在重新生成新 key 之前,RSA key 最多可以使用 30 天。
要使用 key ,您可以调用
rsa.GetKey()
,并注册为签名凭据,请使用:builder.AddSigningCredential(rsa.GetKey());
关于c# - IdentityServer4 的 AddSigningCredential,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49042474/