c# - IdentityServer4 的 AddSigningCredential

标签 c# identityserver4

我们将 IdentityServer4 与 .NET Core Web 应用程序(“http://docs.identityserver.io/en/release/quickstarts/0_overview.html”)一起使用。我们已替换 AddDeveloperSigningCredentialAddSigningCredential(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/

相关文章:

c# - Express PayPal 结账,无法创建 SSL/TLS 安全通道

asp.net - IdentityServer、客户端和用户配置文件管理

identityserver4 - 在ASP.NET Core 2.0中使用IdentityServer4时出现无限身份验证循环

c# - Windows 身份验证不接受凭据

c# - 如何使用 Newtonsoft JSON(反)序列化 XmlException?

c# - List<T> 和 IQueryable<T> 之间的执行差异

c# - 如何在 asp.net core 中获取 .resx 文件字符串

docker - 如何使 Identityserver 重定向到我的网络应用程序?

authentication - Identity Server 4 同意屏幕从不显示

c# - 如何在点后取 6 个数字 - 但不舍入数字?