ASP.Net MVC5:How 加密解密 web.config 中的连接字符串

ASP.Net MVC5:How to encrypt decrypt connection string in web.config

刚看完这篇文章http://tutorialslink.com/Articles/Encrypt-and-Decrypt-Connection-Strings-in-Webconfig/52

他们说如果我们使用这个命令,那么连接字符串将被加密 aspnet_regiis.exe -pef "connectionStrings" "<Path of the Folder containing the Web.Config file>" 并且 假设此命令将解密连接字符串 aspnet_regiis.exe -pdf "connectionStrings" "<Path of the Folder containing the Web.Config file>"

但问题是,如果我们采用这种方法,当我们在不同的电脑上托管我们的网站时,它可能无法正常工作。

所以请告诉我我们应该遵循什么方法来加密/解密 连接字符串或web.config中的任何部分将在任何电脑上工作。

提前致谢。

我不知道您的数据有多敏感,但我想您可以尝试以下操作:

  1. 首先手动加密 connectionString(例如,使用 AES(Rijndael))。
  2. 将加密的字符串粘贴到您的web.config。
  3. 解密代码中的字符串,使用类似这样的东西

    private string getConnectionString()
    {
        string encrypted = System.Configuration.
              ConfigurationManager.AppSettings["connectionString"];
    
        //Rijndael or any other form of decryption here...
        //.....
        //.....
    
        return decryptedString;
    }
    
  4. 使用解密的connectionString连接到你的数据库! :)