gpt4 book ai didi

c# - 正确存储配置字符串

转载 作者:太空狗 更新时间:2023-10-29 22:10:23 24 4
gpt4 key购买 nike

我正在使用 Visual Studio,我对存储配置字符串的最佳方式感到很困惑。我正在创建 Windows 窗体应用程序。我需要非常基本的安全性——我不希望密码在 app.config 中可读,但我不担心有人反汇编我的代码以弄清楚它。

因此,在数据源向导中,我说“不保存密码”,然后我将以下代码放入 Settings.Designer.CS:

public string MyConnectionString {
get {
return ((string)("Data Source=SQLSERVER\\ACCOUNTING;Initial Catalog=ACCOUNTING;User ID=MyUser;Password=28947239SKJFKJF"));
}
}

我知道这不是最好的解决方案,但我想不出更好的解决方案。我将不胜感激任何人对此的帮助和意见。

谢谢--

小姐。

最佳答案

您可以使用 RsaProtectedConfigurationProvider 来加密您的 ConnectionStrings 部分。以下是如何加密和解密此部分的简短示例(请注意,以管理员身份启动 Visual Studio):

主要网络配置:

<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

<connectionStrings>
<add name="MyConnKey" connectionString="Data Source=SQLSERVER\\ACCOUNTING;Initial Catalog=ACCOUNTING;User ID=MyUser;Password=28947239SKJFKJF" />
</connectionStrings>

<appSettings>
<add key="DD" value="567_Access"/>
</appSettings>

</configuration>

代码:

static void Main(string[] args)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
ConfigurationSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;
if (!section.SectionInformation.IsProtected)
{
Console.WriteLine("Protecting connection strings...");
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
}
else
{
Console.WriteLine("Unprotecting connection strings...");
section.SectionInformation.UnprotectSection();
}
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);

var cs = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnKey"];
Console.WriteLine(cs.ConnectionString);

Console.ReadLine();
}

下面是编码配置的样子:

<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyName>Rsa Key</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>js82TLzdIfcdD51g2Us8Nv2eWTSval7oi2Xl+OJsL2c2hUDrm21YG/v1yhuB5Ag8/Uubm9gjmQYcPImo8VOXXDZxEW/HIYNbbkDsopbAyyXNGkHtTrEqz80nqAyipn+Y5QpwXKxFJoaEMPaPdO5juXYd2SPdGaFMBg4m2+drSy6bvXnloz+GIXKbL9QNdxg8br1S8ALUxXsu4F52sKda6J/Sk+I9SBf85XK/JKaHQFoHghf1/m58Zh0hIhci3R6wwGDC3mVG/NcL3tWKpga3ndQ+57FBezsWWOMKyLFPMZG7NkNvBaNG0fYJm2+ApKme1gGil2GGivxySP4evL4hRw==</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>r28s2mSZTEwb99SIJH7kozBR8RkY8LkxzVLm/VExEwc3aLqSJgqJGrNY6f4mtvCT9ZIYV/QjErt9weQNYSZxyou4RXAq1W8yYnzv+7NCvgOgKvAQh/p+iQidh13SmnC6UCtSrMp3HeRSFNj1y1sF2TGYVpWqWA9NAEBsOyYr0ey6S6/fUFrLAy7mcCkawemmDRvxqF7YnG+LoL9Bh59/l++BhTYlMQvz/stHb5mA6bfKgZYbYDA9KEr5mdrr9t8GGzrk3vNW5s723bKZuiqUWiZfWklY2a2NuONDKj4FG3cAUwCdXq2OzIBFVnXSBgrMo+4GCgQar7delms+bvFOnjozrHdKHJLoahithwPEmDuiM4SJJAZHXKTpFrmv4o+YT68i9xs0iUy0p/hnb5lJv3ITCmEnsOmewn7xIsoPcZMEK54kAtoyjXt2H3QR7KdI3Sf4R6X3rHYr4BerF0UatdP8q5ppLi6uYT/epDi0qTFgf9aDuOW2zDc1TYzFEhBrg4sr2DqqTJWgpyI0yVcq76ZmSTwa53ReyvHFuLkMadijJuUe+u5zPj0BDR6kl8vXN0OzXjby8Uw=</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>

<appSettings>
<add key="DD" value="567_Access"/>
</appSettings>

</configuration>

请注意,在这种情况下,解密将仅在首先处理加密的机器上运行。更多信息请访问RsaProtectedConfigurationProvider

关于c# - 正确存储配置字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43313399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com