我在尝试加密app.config中的连接字符串时遇到了问题。 我有一些代码可以保护app.config的connectionStrings部分,但密码仍以明文形式显示。
我需要加密连接字符串,这样在部署时就不会以明文形式显示。我在SO上看到了类似于web.config的问题,但没有针对app.config。
我在尝试加密app.config中的连接字符串时遇到了问题。 我有一些代码可以保护app.config的connectionStrings部分,但密码仍以明文形式显示。
我需要加密连接字符串,这样在部署时就不会以明文形式显示。我在SO上看到了类似于web.config的问题,但没有针对app.config。
你可以轻松地将与web.config相同的解决方案应用于app.config,只需将其重命名为web.config,使用aspnet_regiis工具进行加密,然后将其重新命名为app.config。
%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" c:\<包含你的web.config的文件夹>
(停留在文件夹级别,不要输入尾随的“”)你可以在记事本中打开它以查看已加密的文件。 在Visual Studio中,你会发现它已被解密。 你可以像未加密时一样使用连接字符串。(请注意,它只能在加密的计算机上解密。)
• 将 App.config
文件重命名为 web.config<br>
• 以管理员身份运行命令提示符:
加密操作:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef "connectionStrings"
在引号中输入你的项目位置,并添加 -prov "DataProtectionConfigurationProvider"
例如:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef "connectionStrings" "D:\location\location1\location" -prov "DataProtectionConfigurationProvider"
解密:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pdf "connectionStrings"
将你的项目位置用引号括起来。
例如:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pdf "connectionStrings" "D:\location1\location"
对于错误:
在配置中添加以下内容:xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"
像这样:
• 最后,将 web.config
重命名为 App.Config
此外,如果有人想要在Web Farm中加密和解密连接字符串,请按照以下步骤进行:
Create an RSA key:
aspnet_regiis -pc "MyKeys" -exp
Grant access for application pool identity to this key:
aspnet_regiis -pa "MyKeys" "IIS AppPool\ApplicationPoolName" -full
Add RSA provider to the web.config:
<configuration>
<configProtectedData>
<providers>
<add name="MyProvider"
type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
keyContainerName="MyKeys"
useMachineContainer="true" />
</providers>
</configProtectedData>
</configuration>
Encrypt web.config by using the RSA provider:
aspnet_regiis -pe "connectionStrings" -app "/MyApplication" -prov "MyProvider"
Note: You can use the alternative syntax like the one we did for a single server scenario. Example:
ASPNET_REGIIS -pef "connectionStrings" "D:\inetpub\wwwroot\applicationFolder" -prov "MyProvider"
Open the web.config and confirm that the connection string is encrypted
Test the site and confirm that it is working
Try decrypting the web.config. Create a test.aspx file with the code below inside. Browse it to see the decrypted file
Export the RSA key to the C drive:
aspnet_regiis -px "MyKeys" "c:\keys.xml" -pri
Copy this file to the second server in the web farm
Import it in that server:
aspnet_regiis -pi "MyKeys" "c:\keys.xml"
Grant access to this key (same as Step 2)
Test the application in the second server
来源: 如何加密和解密连接字符串
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
如果你想加密 connectionStrings
config.ConnectionStrings.SectionInformation.ProtectSection(Nothing);
您需要了解应用程序配置部分。
因此,如果您想加密 AppSettings
,
config.AppSettings.SectionInformation.ProtectSection(Nothing);
config.save()
- vibs2006SET ApplicationName=YourAppWithoutExtention
echo.
echo POST BUILD ACTIONS
echo ====================
if EXIST web.config (
echo Deleting web.config
DEL web.config
)
echo Renaming %ApplicationName%.exe.config to web.config
REN %ApplicationName%.exe.config web.config
echo Running aspnet_regis against webconfig
SET rpath=%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" "$(TargetDir)
SET rpath=%rpath:~0,-1%"
echo Path: %rpath%
%rpath%
echo Renaming web.config to %ApplicationName%.exe.config
REN web.config %ApplicationName%.exe.config
echo Done.
将“YourAppWithoutExtention”替换为您的应用程序名称。
然后每次构建时,它都会自动加密您的app.config文件。
var keyVaultUrl = "https://<your-key-vault-name>.vault.azure.net/";
var credential = new DefaultAzureCredential();
var client = new SecretClient(vaultUri: new Uri(keyVaultUrl), credential);
KeyVaultSecret secret = client.GetSecret("<your-secret-name>");
Console.WriteLine($"{secret.Name}: {secret.Value}");
对于 AWS Secrets Manager(省略了一些错误处理代码):
var client = new AmazonSecretsManagerClient(accessKeyId, secretAccessKey,
RegionEndpoint.APSoutheast2);
var request = new GetSecretValueRequest {
SecretId = secretName
};
GetSecretValueResponse response = null;
response = client.GetSecretValueAsync(request).Result;
您还可以搜索替代的秘密管理器和实现,如Google Cloud Secret Manager或其他。
与本地存储秘密相比,这种方法有很多优点:
您不必在Prod / Staging / Dev环境的配置文件中存储不同的值 - 只需读取适当命名的密钥(例如“[Dev | Prod | Stag]DBPassword”)
仅有少数人可以访问非常重要的秘密(例如,我不知道,转移全球Deus账户中所有$$$到E-Coin钱包的授权码),他们的访问权限可以随时撤销
如果有人窃取了您的源代码(不满的员工,意外泄漏),则没有任何密码泄漏
更改密码很容易 - 您只需使用could管理控制台更新它并重新启动应用程序
如何使用AWS Secrets Manager在.Net Core应用程序中存储和读取密码
.NET Core 应用程序中如何使用 Azure Key Vault 安全地存储和检索敏感信息
鸣谢并感谢 @smartial-arts参考资料;第二个答案。