- Создать ключ RSA в Azure Key Vault --> Ключи
- Получить ключ с помощью KeyClient
- Передайте ключ в AddSigningCredential
string keyVaultUrl = Configuration.GetValue("AzureSettings:KeyVaultUrl");
var identityServerConfig = Configuration.GetSection("IdentityServer:Key");
string keyName = identityServerConfig["Name"];
var client = new KeyClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
KeyVaultKey keyVaultKey = client.GetKey(keyName);
// Ensure the key is an RSA key
if (keyVaultKey.KeyType != KeyType.Rsa && keyVaultKey.KeyType != KeyType.RsaHsm) {
throw new InvalidOperationException("The key retrieved from Key Vault is not an RSA key.");
}
// Convert KeyVault's RSA key to RsaSecurityKey
var rsa = new RSAParameters { Modulus = keyVaultKey.Key.N, Exponent = keyVaultKey.Key.E };
var rsaCryptoServiceProvider = RSA.Create();
rsaCryptoServiceProvider.ImportParameters(rsa);
RsaSecurityKey key = new(rsaCryptoServiceProvider);
identityServiceBuild.AddSigningCredential(key, IdentityServerConstants.RsaSigningAlgorithm.RS256);
Вот как создается ключ RSA в Azure Key Vault.
[img]https://i .sstatic.net/tJuIWYyf.png[/img]
Вот выброшенное исключение:(
[01:51:18 Information] UnhandledExceptionEvent { Details: "System.Security.Cryptography.CryptographicException: Unknown error (0xc100000d)
at System.Security.Cryptography.RSABCrypt.TrySignHash(ReadOnlySpan`1 hash, Span`1 destination, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding, Int32& bytesWritten)
at System.Security.Cryptography.RSA.TrySignData(ReadOnlySpan`1 data, Span`1 destination, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding, Int32& bytesWritten)
at Microsoft.IdentityModel.Tokens.AsymmetricAdapter.SignUsingSpanRsa(ReadOnlySpan`1 data, Span`1 destination, Int32& bytesWritten)
at Microsoft.IdentityModel.Tokens.AsymmetricAdapter.SignUsingSpan(ReadOnlySpan`1 data, Span`1 destination, Int32& bytesWritten)
at Microsoft.IdentityModel.Tokens.AsymmetricSignatureProvider.Sign(ReadOnlySpan`1 input, Span`1 signature, Int32& bytesWritten)
at Microsoft.IdentityModel.JsonWebTokens.JwtTokenUtilities.CreateSignature(ReadOnlySpan`1 data, Span`1 destination, SigningCredentials signingCredentials, Int32& bytesWritten)
Я также пытался сгенерировать ключ RSA с помощью генератора ключей Putty и загрузить его, но было выдано то же исключение.
Обратитесь к сертификату на C#, например этому @Tore?
string keyVaultUrl = Configuration.GetValue("AzureSettings:KeyVaultUrl");
var client = new CertificateClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
KeyVaultCertificateWithPolicy certificate = client.GetCertificate("duendeidentityserverv7");
if (certificate == null) {
throw new InvalidOperationException($"Certificate duendeidentityserverv7 not found in Azure Key Vault.");
}
X509Certificate2 cert = new X509Certificate2(certificate.Cer);
identityServiceBuild.AddSigningCredential(cert);
Подробнее здесь: https://stackoverflow.com/questions/791 ... -server-v7