Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何安全地存储和访问连接字符串详细信息?_C#_Asp.net Mvc 3_Database Connection_Connection String - Fatal编程技术网

C# 如何安全地存储和访问连接字符串详细信息?

C# 如何安全地存储和访问连接字符串详细信息?,c#,asp.net-mvc-3,database-connection,connection-string,C#,Asp.net Mvc 3,Database Connection,Connection String,我目前正在一个ASP.NETMVC网站上工作,我已经到了需要将数据库集成到该网站的地步 通常,我只需将适当的连接字符串添加到Web.config文件: <add name="MainDB" connectionString="Server=localhost; Database=TopSecretData; User Id=Joe; password=password" providerName="System.Data.SqlClient" /> 但是,如果

我目前正在一个ASP.NETMVC网站上工作,我已经到了需要将数据库集成到该网站的地步

通常,我只需将适当的连接字符串添加到
Web.config
文件:

<add name="MainDB" 
    connectionString="Server=localhost; Database=TopSecretData; User Id=Joe;
    password=password" providerName="System.Data.SqlClient" />

但是,如果我将用户Id和密码保留在
Web.config
中,显然会有一个明显的安全漏洞,尤其是在受源代码控制的情况下

简而言之:如何存储连接字符串详细信息而不使其公开可见?常用方法包括和


第二个链接是一篇涉及如何正确保护ASP.NET应用程序的更大文章的一部分。它是为WebForms编写的,但原则是一样的。这是一本很好的读物,即使它有点旧,但大部分内容在今天仍然适用。

也许您想研究一下加密连接字符串:
(文章有点老)

一种方法是使用DB提供的任何集成安全性,因此密码不是问题。服务器无需使用密码即可直接访问服务器,但您必须设置一个只能从web服务器本身访问的用户

像MySQL这样的数据库允许你指定哪些服务器可以访问它,限制从其他任何地方的访问——因此黑客只能从web服务器访问你的数据库。这大大降低了安全性,并允许您在SCM中存储连接字符串文件

它仍然不是100%安全的,因为黑客可以(通常很容易)攻击您的web服务器并从中查看数据库。您可以将密码存储在其他地方,但这只是掩盖了问题——如果web服务器可以访问密码,您的黑客也可以。(注意,存储密码的其他位置包括注册表、一个单独的文件(如.udl文件)或/etc中的某个文件)。您可以保护此文件,以便只有web服务器用户可以读取它,但被黑客攻击的web服务器显然可以读取它

因此,下一步是抽象DB连接,使其位于web服务器之外,通常的方法是使用一个单独的进程来存储业务逻辑(例如,一个服务),它公开固定的方法—web服务器只需调用执行工作的服务并将数据返回给web服务器代码

如果黑客击败了您的web服务器,他们所能做的就是调用服务上的方法,他们将无法直接访问数据库,因此无法损坏或修改它。通常情况下,对于服务方法是什么或做了什么,很少有提示给黑客,而且服务的所有输入都有相当数量的验证代码,因此黑客创建的消息(希望)会被拒绝。(使用时间戳、计数器等尝试阻止发送给服务的定制消息)


这是我们用于高安全性系统的方法(使用标准操作系统安全机制,您可以做更多的事情来保护此链的每个部分)。一旦我们的安全伙伴演示了一个IIS黑客,给了他一个具有管理员权限的远程shell,我们就清楚了这样做的原因。如果黑客得到了这些信息,那么在web服务器上保护配置的任何操作都是毫无意义的。(这很容易做到——因为已修复,但始终存在0天漏洞)

最佳做法是加密连接字符串部分。使用aspnet_regiis.exe,它可以在不同的地方找到:

  • 开始–Visual Studio–Visual Studio工具–Visual Studio命令提示符
  • C:\Windows\Microsoft.NET\Framework\v4.0.30319(确保以管理员身份运行)
之前:

<configuration>
<connectionStrings>
<add name="MainConnectionString" 
 connectionString="data source=Ratbert;database=Sales;username=ASPNET;password=$Double_Rainbow2011" 
 providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
或者,如果上面的命令不起作用(并且您得到了aspnet\u regais帮助文本),请尝试

其中,“6”是IIS中报告的站点ID

之后:

<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
  <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
   xmlns="http://www.w3.org/2001/04/xmlenc#">
   <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
   <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
    <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
     <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
     <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
      <KeyName>Rsa Key</KeyName>
     </KeyInfo>
     <CipherData>
      <CipherValue>Bf677iFrUFW ... +4n4ZZKXCTUAu2Y=</CipherValue>
     </CipherData>
    </EncryptedKey>
   </KeyInfo>
   <CipherData>
    <CipherValue>UDEZ ...QfXUmM5rQ==</CipherValue>
   </CipherData>
  </EncryptedData>
 </connectionStrings>

然后更改并重新加密

要读取连接字符串,请使用ConfigurationManager静态类

string connStr = 
ConfigurationManager
.Connectionstrings["MainConnectionString"]
.ConnectionString.ToString();

var myConnection = new SqlConnection(connStr);

myConnection.Open();

您可以在缓存中存储加密的连接字符串。缓存服务器故意位于另一台服务器上(这种通信可以限制为1个端口和IP地址,使黑客更难进行攻击)。这将使连接字符串完全脱离web服务器,即使黑客能够访问缓存,它们也会被加密。关键是将字符串加载到缓存中,这可以远程完成,因此这些连接字符串永远不会写入服务器的硬盘驱动器。该代码仅根据需要解密连接字符串,从不保留变量中未加密的字符串

太好了!这正是我要找的。
<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
  <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
   xmlns="http://www.w3.org/2001/04/xmlenc#">
   <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
   <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
    <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
     <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
     <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
      <KeyName>Rsa Key</KeyName>
     </KeyInfo>
     <CipherData>
      <CipherValue>Bf677iFrUFW ... +4n4ZZKXCTUAu2Y=</CipherValue>
     </CipherData>
    </EncryptedKey>
   </KeyInfo>
   <CipherData>
    <CipherValue>UDEZ ...QfXUmM5rQ==</CipherValue>
   </CipherData>
  </EncryptedData>
 </connectionStrings>
aspnet_regiis –pdf connectionStrings c:\PathToWebSite
aspnet_regiis -pd connectionStrings -app "/" -site 6
string connStr = 
ConfigurationManager
.Connectionstrings["MainConnectionString"]
.ConnectionString.ToString();

var myConnection = new SqlConnection(connStr);

myConnection.Open();