Rya*_*one 3 c# asp.net encryption asp.net-core-mvc asp.net-core-2.0
我想使用我的appsettings.json来存储“主密码”。
然后,将使用此主密码来打开由这个出色的密码存储包生成的私钥(及其后续的密码存储):https : //github.com/neosmart/SecureStore
问题是,我想不出任何方式来加密主密码。我知道在.NET 4.5中,可以执行以下操作:
1)将密码放入web.config文件
2)运行此脚本:aspnet_regiis.exe -pef appSettings“ C:\ myfolder”
3)您的密码最终将被加密-但可以被程序安全地读取。
https://www.codeproject.com/Articles/599416/Encrypting-ASP-NET-Application-Settings
我要以正确的方式这样做还是有更好的做法?
请记住,不要在网站的主目录appsettings.json中存储机密,通常在源代码控制中。使用文件提供程序将文件定位在服务器上其他位置。
如果有权访问Azure,则可以将机密存储在Azure Key Vault中,而不是appsettings.json。
考虑到这一点,如果要使用JSON文件,则可以使用网桥或代理类来处理值的解密。
首先,您将需要一个类来解密值。为简便起见,在这里我将不讨论解密类的详细信息,而只是假设SettingsDecryptor已编写了一个名为的类,ISettingsDecryptor并使用单个方法Decrypt来实现一个调用该接口的方法,该方法对字符串值进行解密。
bridge类采用两个构造函数参数。
IOptions<T>or IOptionsSnapshot<T>,其中T是appsettings.json 通过services.Configure方法(Eg MyAppSettings)将节绑定到的类。另外,如果您不想绑定到类,则可以IConfiguration改用并直接从配置中读取。ISettingsDecryptor。在网桥类中,每个需要解密的属性都应使用解密类来解密配置中的加密值。
public class MyAppSettingsBridge : IAppSettings
{
private readonly IOptions<MyAppSettings> _appSettings;
private readonly ISettingsDecrypt _decryptor;
public MyAppSettingsBridge(IOptionsSnapshot<MyAppSettings> appSettings, ISettingsDecrypt decryptor) {
_appSettings = appSettings ?? throw new ArgumentNullException(nameof(appSettings));
_decryptor = decryptor ?? throw new ArgumentException(nameof(decryptor));
}
public string ApplicationName => _appSettings.Value.ApplicationName;
public string SqlConnectionSting => _decryptor.Decrypt(_appSettings.Value.Sql);
public string OracleConnectionSting => _decryptor.Decrypt(_appSettings.Value.Oracle);
}
Run Code Online (Sandbox Code Playgroud)
DI容器应设置如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
services.Configure<MyAppSettings>(Configuration.GetSection("MyAppSettings"));
services.AddSingleton(Configuration);
services.AddSingleton<ISettingsDecrypt, SettingsDecryptor>();
services.AddScoped<IAppSettings, MyAppSettingsBridge>();
}
Run Code Online (Sandbox Code Playgroud)
然后,控制器可以具有一个构造函数,该构造函数将桥接器作为IAppSettings来访问解密的设置。
上面的答案是整个解决方案的简短摘要,因为需要大量代码。
可以在我的博客文章“ 隐藏秘密appsettings.json-在ASP.Net Core配置中使用网桥(第4部分)”中看到完整的详细说明,在此我详细描述了使用网桥模式。Github上还有一个完整的示例(包括解密类),位于https://github.com/configureappio/ConfiguarationBridgeCrypto
JSON配置提供程序不支持加密。当前,唯一支持加密配置的现成提供程序是Azure KeyVault。无论您的应用程序是否实际托管在Azure上,您都可以使用KeyVault,尽管它不是免费的,但这种津贴使得在大多数情况下只需要花费几便士。
就是说,Core的部分美在于它是完全模块化的。您始终可以创建自己的配置提供程序并实现所需的任何内容。例如,你可以写一个JSON提供商,实际上不支持加密,如果这就是你想去的地方。
| 归档时间: |
|
| 查看次数: |
9931 次 |
| 最近记录: |