gpt4 book ai didi

.net - 如何在 WinForms 应用程序中安全地存储连接字符串?

转载 作者:行者123 更新时间:2023-12-02 18:15:14 24 4
gpt4 key购买 nike

我需要知道在 VB.NET 中存储 WinForms 应用程序的 SQL 服务器连接字符串的常用方法是什么。

我在网上搜索并找到了以下每个问题的答案:

  • 如何读取 app.config 值
  • 如何在 ASP.NET 中实现引用:this SO question .
  • 如何存储连接字符串(未加密,因此不安全)

我想要一个完整的答案,了解如何在 app.config(或 settings.settings,如果更好)中安全地存储 VB.NET 中的连接字符串。

app.config 位置正确吗?我可以加密这些值吗?

最佳答案

简单地说,.net 框架允许您做到这一点,请参阅

http://msdn.microsoft.com/en-us/library/89211k9b(v=vs.80).aspx

相关信息:

这会进入ma​​chine.config文件:

<configProtectedData defaultProvider="RsaProtectedConfigurationProvider">
<providers>
<add name="RsaProtectedConfigurationProvider"
type="System.Configuration.RsaProtectedConfigurationProvider, ... />
<add name="DataProtectionConfigurationProvider"
type="System.Configuration.DpapiProtectedConfigurationProvider, ... />
</providers>
</configProtectedData>

这是应用程序代码:

Shared Sub ToggleConfigEncryption(ByVal exeConfigName As String)
' Takes the executable file name without the
' .config extension.
Try
' Open the configuration file and retrieve
' the connectionStrings section.
Dim config As Configuration = ConfigurationManager. _
OpenExeConfiguration(exeConfigName)

Dim section As ConnectionStringsSection = DirectCast( _
config.GetSection("connectionStrings"), _
ConnectionStringsSection)

If section.SectionInformation.IsProtected Then
' Remove encryption.
section.SectionInformation.UnprotectSection()
Else
' Encrypt the section.
section.SectionInformation.ProtectSection( _
"DataProtectionConfigurationProvider") 'this is an entry in machine.config
End If

' Save the current configuration.
config.Save()

Console.WriteLine("Protected={0}", _
section.SectionInformation.IsProtected)

Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub

更新1

感谢@wpcoder this link

关于.net - 如何在 WinForms 应用程序中安全地存储连接字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10606892/

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