【发布时间】:2022-01-15 23:12:04
【问题描述】:
我正在尝试使用 ASP.NET Core DataProtection 设置 OpenIddict,但是在尝试启动应用程序时我不断收到异常:
InvalidOperationException: At least one encryption key must be registered in the OpenIddict server options. Consider registering a certificate using 'services.AddOpenIddict().AddServer().AddEncryptionCertificate()' or 'services.AddOpenIddict().AddServer().AddDevelopmentEncryptionCertificate()' or call 'services.AddOpenIddict().AddServer().AddEphemeralEncryptionKey()' to use an ephemeral key.
现在我明白错误要我做什么了,我需要添加一个签名密钥/加密证书,但是为什么呢?
使用 DataProtection 的重点不是这部分是自动化的,不需要您添加显式签名密钥/加密证书吗? 根据文档,应使用 DataProtection 加密除 JWT id 令牌之外的所有密钥。
这是当前代码:
if (authenticationOptions.DataProtection.TokenProtection.Enabled)
services
.AddDataProtection(options =>
{
options.ApplicationDiscriminator = applicationOptions.ShortName;
})
.SetApplicationName(applicationOptions.Name)
.SetDefaultKeyLifetime(TimeSpan.FromDays(authenticationOptions.DataProtection.TokenProtection.LifeTime))
.PersistKeysToAzureBlobStorage(CreateDataProtectionBlobClient(azureOptions.BlobStorageUrl, authenticationOptions.DataProtection, azureCredential))
.ProtectKeysWithAzureKeyVault(completeKeyVaultUri, azureCredential);
}
services.AddOpenIddict()
.AddCore(options =>
{
options.UseEntityFrameworkCore()
.UseDbContext<ApplicationDbContext>()
.ReplaceDefaultEntities<long>(); ;
})
.AddServer(options =>
{
options.SetAuthorizationEndpointUris("connect/auth");
options.SetTokenEndpointUris("/connect/token");
options.AllowAuthorizationCodeFlow();
options.AllowClientCredentialsFlow();
options.AllowRefreshTokenFlow();
options.UseAspNetCore()
.EnableAuthorizationEndpointPassthrough()
.EnableTokenEndpointPassthrough();
options.UseDataProtection();
//options.AddEphemeralSigningKey();
//options.AddEphemeralEncryptionKey();
})
.AddValidation(options =>
{
options.UseLocalServer();
options.UseAspNetCore();
options.UseDataProtection();
});
【问题讨论】: