2011-09-03 128 views
1

我瞭解在.net v4中加密連接字符串的可能性。我有一個勝利形式的應用程序,將被不同機器上的多個人使用。我知道我需要在應用程序首次在目標機器上運行時保護連接字符串。不過,我擔心一段時間後,我的連接字符串將被解密。我正在尋找如何在安裝過程中使用已加密或加密的連接字符串來部署我的應用程序的建議。vb.net連接實體框架連接字符串安全

其他人將如何以安全的方式加密連接字符串?

任何幫助是極大的讚賞。

+0

如果允許使用該應用程序的人員看到連接字符串,可以嗎?還是應該保密,甚至從他們? –

+0

最好保守祕密。 – WizardsSleeve

+2

這個問題爲什麼被低估? downvoting時請留言。這對我來說似乎是一個很好的問題。 –

回答

1

你可以得到你的connectionString配置節和modify it,這樣的:

oSection = oConfiguration.GetSection("connectionStrings") as System.Configuration.ConnectionStringsSection; 

if (oSection != null) 
{ 
    if ((!(oSection.ElementInformation.IsLocked)) && (!(oSection.SectionInformation.IsLocked))) 
    { 
     if (protect) 
     { 
      if (!(oSection.SectionInformation.IsProtected)) 
      { 
       blnChanged = true; 

       // Encrypt the section. 
       oSection.SectionInformation.ProtectSection 
          (strProvider); 
      } 
     } 
     else 
     { 
      if (oSection.SectionInformation.IsProtected) 
      { 
       blnChanged = true; 

       // Remove encryption. 
       oSection.SectionInformation.UnprotectSection(); 
      } 
     } 
    } 

    if (blnChanged) 
    { 
     // Indicates whether the associated configuration section 
     // will be saved even if it has not been modified. 
     oSection.SectionInformation.ForceSave = true; 

     // Save the current configuration. 
     oConfiguration.Save(); 
    } 
} 

Example in VB.NET

Public Sub ProtectSection() 
    ' Get the current configuration file. 
    Dim config As Configuration = ConfigurationManager.OpenExeConfiguration 
        (ConfigurationUserLevel.None) 
    Dim protectedSection As ConfigurationSection = config.GetSection(m_Section) 

    ' Encrypts when possible 
    If ((protectedSection IsNot Nothing) _ 
    AndAlso (Not protectedSection.IsReadOnly) _ 
    AndAlso (Not protectedSection.SectionInformation.IsProtected) _ 
    AndAlso (Not protectedSection.SectionInformation.IsLocked) _ 
    AndAlso (protectedSection.SectionInformation.IsDeclared)) Then 
     ' Protect (encrypt)the section. 
     protectedSection.SectionInformation.ProtectSection(Nothing) 
     ' Save the encrypted section. 
     protectedSection.SectionInformation.ForceSave = True 
     config.Save(ConfigurationSaveMode.Full) 
    End If 
End Sub 

您可以安裝您的應用程序中使用此代碼(檢查,是配置保護),並且您可以在運行應用程序時檢查每次。

UPDATE:
關於從評論你的問題 - 你可以用空連接字符串發佈應用程序,並在您的安裝,設置該屬性(不要忘了在這種情況下,代碼混淆),並保存您的配置文件。

+0

謝謝你的回答。稍微偏離主題,但是您將使用什麼類型的安裝程序來分發將使用安裝程序保護連接字符串的應用程序?謝謝 – WizardsSleeve

+0

@WizardsSleeve更新了問題。 – VMAtm