Dot-Net

如何在 WinForms 應用程序中安全地儲存連接字元串?

  • August 5, 2017

我需要知道在 VB.NET 中為 WinForms 應用程序儲存 SQL 伺服器連接字元串的常用方法是什麼。

我搜尋了網路,並找到了以下每個問題的答案:

  • 如何讀取 app.config 值
  • 如何在 ASP.NET參考中做到這一點:this SO question
  • 如何儲存連接字元串(未加密因此不安全)

我想要一個關於如何在 VB.NET 中安全地app.config(或者settings.settings如果更好的話)儲存連接字元串的完整答案。

app.config正確的地方嗎?我可以加密這些值嗎?

簡單地說,.net 框架允許您這樣做,請參閱

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

相關資訊:

這進入machine.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,對於這個連結

引用自:https://stackoverflow.com/questions/10606892