0

我已經使用DataProtectionConfigurationProvider來加密web配置的連接字符串,它在本地工作正常。使用DataProtectionConfigurationProvider的Web配置加密在生產上不起作用

但是當我將代碼上傳到生產環境時,Web配置沒有得到加密。

我用下面的代碼:

Configuration config = 
       WebConfigurationManager.OpenWebConfiguration("/"); 
      // Let's work with the <connectionStrings> section 
      ConfigurationSection connectionStrings = config.GetSection("connectionStrings"); 
      if (connectionStrings != null) 
      { 

       // Only encrypt the section if it is not already protected 
       if (!connectionStrings.SectionInformation.IsProtected) 
       { 
        // Encrypt the <connectionStrings> section using the 
        // DataProtectionConfigurationProvider provider 
        connectionStrings.SectionInformation.ProtectSection(
         "DataProtectionConfigurationProvider"); 
        config.Save(); 
       } 
      } 

我通過將日誌跟蹤的代碼,發現connectionStrings.SectionInformation.IsProtected條件不工作!

任何幫助將是可觀的!

回答

0

問題出在WebConfigurationManager.OpenWebConfiguration(「/」)中的「/」路徑。我的應用程序託管在虛擬目錄中。

使用下面的代碼來解決這個問題:

Configuration config = WebConfigurationManager.OpenWebConfiguration("~");