2013-09-23 59 views
1

對不起,如果這看起來像一個愚蠢的問題。如何防止閱讀app.config?

我的應用程序使用連接字符串連接到SQL Server 2008數據庫並使用Crystal Report,我的服務器使用混合身份驗證模式。

問題是:app.config文件顯示連接字符串(用戶名&密碼),我不想讓任何人看到!

Data Source=.\SQLEXPRESS;Initial Catalog=ComplainsDb;Persist Security Info=false; User ID=abcde ;Password=MyPassword 

感謝您的任何幫助。

+5

加密在app.config。谷歌它,你會發現很多關於它.. –

+0

相關[問題](http://stackoverflow.com/questions/11637348/encrypt-connection-string-in-app-config) –

回答

2

您需要的時候你的程序開始調用是這樣的:

void EncryptConnectionStringsIfNecessary() 
    { 
     var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     ConnectionStringsSection section = configFile.ConnectionStrings; 
     if (section != null) 
     { 
      if (!section.IsReadOnly()) 
      { 
       if (!section.SectionInformation.IsProtected) 
       { 
        section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); 
        section.SectionInformation.ForceSave = true; 
        configFile.Save(ConfigurationSaveMode.Full); 
       } 
      } 
     } 
    } 
+0

到目前爲止,這是我的工作。 。但是我怎麼能解密這個文件? –

+1

你通常不需要做任何事情。如[MSDN文檔](http://msdn.microsoft.com/en-us/library/hh8x3tas.aspx)中所述,.NET框架將在您訪問'ConfigurationManager.ConnectionStrings [connectionStringName]時自動解密設置' 。 – shamp00

+0

非常感謝每一位幫助我 –