【问题标题】:ASP.Net MVC5:How to encrypt decrypt connection string in web.configASP.Net MVC5:如何在 web.config 中加密解密连接字符串
【发布时间】:2018-09-23 18:48:52
【问题描述】:

阅读这篇文章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 中的任何部分,这将适用于任何电脑。

提前致谢。

【问题讨论】:

  • 你为什么要首先这样做?无论如何,连接字符串应该对外部隐藏。
  • 我想这样做,即使任何人都可以破解 web.config 他们将无法轻松读取数据库连接详细信息。

标签: asp.net encryption web-config


【解决方案1】:

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

  1. 首先手动加密连接字符串(例如,使用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. 使用解密的连接字符串连接到您的数据库! :)

【讨论】:

  • 我想使用 asp.net 中内置的任何东西来自动加密和解密数据。感谢您的回复。
  • 粘贴加密字符串后,您不必担心 :) 此外,.NET Framework 中内置了 AES 加密
  • 您所解释的方法是正常的加密解密操作,但我正在寻找 asp.net 代表开发人员执行此操作的方法。无论如何感谢您的回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 2010-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-18
相关资源
最近更新 更多