.NET Core 允许您将机密存储在配置系统的各个位置。
documentation 中列出了许多不同的配置提供程序:
- 文件格式(INI、JSON 和 XML)。
- 命令行参数。
- 环境变量。
- 内存中的 .NET 对象。
- 未加密的 Secret Manager 存储。
- 加密的用户存储,例如 Azure Key Vault。
- 自定义提供程序(安装或创建)。
在命令行上使用 Microsoft.Extensions.SecretManager.Tools 管理用户密码很方便,然后使用 Microsoft.Extensions.Configuration.UserSecrets 从应用配置中获取密码。
但是,您应该自己加密存储的信息,无论您将其存储在何处,例如。 appsettings.production.json。
为此,您可以使用自定义配置提供程序来加密和解密您的秘密。您的自定义提供程序应继承自 ConfigurationProvider 类,如 here 所示。
public class CustomConfigProvider : ConfigurationProvider
{
public CustomConfigProvider() { }
public override void Load()
{
Data = MyEncryptUtils.DecryptConfiguration();
}
}
另见:Encrypted Configuration in ASP.NET Core