[英]Using IdentityServer4 with DataProtection API for token signing
我有一個基於 IS4 + Net Core 3.1 的身份驗證/授權服務器。 由於此身份服務與一些新的以及一些非常舊的應用程序交互,它還會創建 2 個不同的身份驗證 cookie 和令牌。 我們目前正在使用 DPAPI 來簽署 cookie 和我們需要的一些其他敏感信息。
到目前為止,我們一直在 IS4 中使用證書作為 SigningCredential,但現在我希望能夠使用 DPAPI 以便自動管理密鑰輪換,或者至少,我們可以在一個地方管理它們。
基於IS4 文檔,我只需要實現ISigningCredentialStore
和IValidationKeysStore
,但是我不太熟悉這是如何管理的。 我有一個這樣的近似值(我知道,在這一點上它幾乎毫無價值):
private class CustomSigningCredentialStore : ISigningCredentialStore
{
private readonly IKeyManager _keyManager;
public CustomSigningCredentialStore(IKeyManager keyManager) =>
_keyManager = keyManager;
public Task<SigningCredentials> GetSigningCredentialsAsync()
{
// For the sake of simplicity, lets assume
// that this is the correct one
var currentKey = _keyManager.GetAllKeys().ElementAt(0);
// Complete
return new SigningCredentials(...);
}
}
private class CustomValidationKeysStore : IValidationKeysStore
{
private readonly IKeyManager _keyManager;
public CustomValidationKeysStore(IKeyManager keyManager) =>
_keyManager = keyManager;
public Task<IEnumerable<SecurityKeyInfo>> GetValidationKeysAsync()
{
var keys = _keyManager.GetAllKeys();
// Complete
return keys.Select(key => new SecurityKeyInfo(...));
}
}
老實說,我堅持這個,我不知道如何映射 DPAPI 生成的當前密鑰,它看起來像這樣:
<?xml version="1.0" encoding="utf-8"?>
<key id="32f6d43e-97a9-4c4b-b108-e11f65e05cc4" version="1">
<creationDate>2020-07-31T00:00:00.0886417Z</creationDate>
<activationDate>2020-07-31T00:00:00.086065Z</activationDate>
<expirationDate>2020-08-30T00:00:00.086065Z</expirationDate>
<descriptor deserializerType="Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=3.1.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60">
<descriptor>
<encryption algorithm="AES_256_CBC" />
<validation algorithm="HMACSHA256" />
<masterKey p4:requiresEncryption="true" xmlns:p4="http://schemas.asp.net/2015/03/dataProtection">
<value>h1/a9G8i78kpnHBTEsisA5rk5ppm8vzByquAT2CO1OFnYHCX/eT/yfcFM/JigYkSDj+dupBz9ZZ7/XFMu/uWxg==</value>
</masterKey>
</descriptor>
</descriptor>
</key>
任何見解將不勝感激。
我最近實現了我自己的密鑰環存儲,將密鑰存儲在 Azure Key Vault 中,我在這里寫了博客:
將 ASP.NET Core 數據保護密鑰環存儲在 Azure Key Vault 中
對於您詢問的令牌登錄,我首先從 Azure Key Vault 加載它們,然后手動將其添加到 IdentitySever,如下所示(使用一些自定義擴展方法):
// First we load the certificates (that contains the private keys) from Azure Key Vault
var rsaCert = KeyVaultExtensions.LoadCertificate(config, "rsa");
//Add RS256 (RSASSA-PKCS1-v1_5 using SHA-256)
builder.AddSigningCredential(rsaCert, "RS256");
此外,我不會嘗試使用 DPAPI 形式的密鑰作為簽名密鑰,因為這些密鑰不適合用於唱令牌。 對於令牌簽名,您需要 RSA 或 ECDSA 密鑰,例如,最簡單的方法是將它們存儲在 Azure Key Vault 中。 您應該在應用程序之外配置和存儲 DPAPI,但這僅用於保護會話 cookie。 不簽署令牌。
我非常懷疑您是否可以使用數據保護來管理和輪換簽名密鑰。 DPAPI 發布並存儲在密鑰環中的密鑰是對稱的,而用於密鑰簽名的密鑰需要是非對稱的(公鑰/私鑰 RSA/ECDSA 密鑰)。 基本上,它們不兼容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.