将.cs文件中的机密设置存储为Web.config是否安全?
我想在Setting.cs(以及静态类)文件中存储一些机密值(例如数据库连接字符串,一些密码)。将.cs文件中的机密设置存储为Web.config是否安全?
是100%确定,IIS 7不以纯文本格式提供此文件吗? 或者是否有任何已知的漏洞?
为了解密和查看加密的web.config文件,您必须拥有对服务器的物理访问权限,管理员访问权限以及对文件本身的访问权限。你必须在这里作出一个假设,即可以做到这一点的人(即你的IT部门)实际上是值得信赖的。 – 2010-07-01 11:52:03
我会建议不要硬编码连接字符串。 IIS不会为他们提供服务,有向ASP.NET注册的明确的处理程序,它将停止提供源代码文件。
你不能使用加密的web.config文件吗?
添加ClassLibrary进行加密并解密您的连接信息。 这是我的密码学服务代码。使用您的连接设置加密
System.Security.Cryptography下;命名空间
public class TripleDes
{
readonly byte[] _key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
readonly byte[] _iv = { 8, 7, 6, 5, 4, 3, 2, 1 };
// define the triple des provider
private readonly TripleDESCryptoServiceProvider _mDes = new TripleDESCryptoServiceProvider();
// define the string handler
private readonly UTF8Encoding _mUtf8 = new UTF8Encoding();
// define the local property arrays
private readonly byte[] _mKey;
private readonly byte[] _mIv;
/// <summary>
/// Default constructor
/// </summary>
public TripleDes()
{
_mKey = _key;
_mIv = _iv;
}
/// <summary>
/// Parameterized constructor
/// </summary>
/// <param name="key"></param>
/// <param name="iv"></param>
public TripleDes(byte[] key, byte[] iv)
{
_mKey = key;
_mIv = iv;
}
/// <summary>
/// Encrypts the given byte array input
/// </summary>
/// <param name="input">Input value</param>
/// <returns>Encrypted result</returns>
public byte[] Encrypt(byte[] input)
{
return Transform(input, _mDes.CreateEncryptor(_mKey, _mIv));
}
/// <summary>
/// Decrypts the given encrypted byte array input
/// </summary>
/// <param name="input">Encrypted byte array input</param>
/// <returns>Decrypted result</returns>
public byte[] Decrypt(byte[] input)
{
return Transform(input, _mDes.CreateDecryptor(_mKey, _mIv));
}
/// <summary>
/// Encrypts the given string input
/// </summary>
/// <param name="text">Input value</param>
/// <returns>Encrypted result</returns>
public string Encrypt(string text)
{
byte[] input = _mUtf8.GetBytes(text);
byte[] output = Transform(input, _mDes.CreateEncryptor(_mKey, _mIv));
return Convert.ToBase64String(output);
}
/// <summary>
/// Decrypts the given encrypted string input
/// </summary>
/// <param name="text">Encrypted string input</param>
/// <returns>Decrypted result</returns>
public string Decrypt(string text)
{
byte[] input = Convert.FromBase64String(text);
byte[] output = Transform(input, _mDes.CreateDecryptor(_mKey, _mIv));
return _mUtf8.GetString(output);
}
private static byte[] Transform(byte[] input, ICryptoTransform cryptoTransform)
{
// create the necessary streams
using (MemoryStream memStream = new MemoryStream())
{
using (CryptoStream cryptStream = new CryptoStream(memStream, cryptoTransform, CryptoStreamMode.Write))
{
// transform the bytes as requested
cryptStream.Write(input, 0, input.Length);
cryptStream.FlushFinalBlock();
// Read the memory stream andconvert it back into byte array
memStream.Position = 0;
byte[] result = memStream.ToArray();
// close and release the streams
memStream.Close();
cryptStream.Close();
// hand back the encrypted buffer
return result;
}
}
}
}
.cs文件扩展名未提供。如果您在Windows \ Microsoft.NET \ framework \ framework version \ config下查看web.config,则可以搜索httpforbiddenhandler
。对于的.cs添加以下行(.NET 4.0是我把它从版本):
<add path="*.cs" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
编辑:
您还可以使用书面定制的ProtectedConfigurationProvider添加更多保护为你的连接关系。
另外,尽管.cs文件不会由IIS提供,但将这些设置放入Web.config文件是一种更好的做法。在Web.config中更改值比更新代码并重新编译/部署程序集更容易。 Web.config文件也不会由IIS提供,因此它同样安全。 – 2010-07-01 11:31:27
加密的web.config是去获得最大的安全性...
即使你混淆您的组件的方式,这些值能被找到。如果您控制自己的Web服务器而其他人无法访问,则它们在代码和配置中都是安全的,但例如在共享主机环境中,域管理器会自动访问,并且您依赖其他用户无法访问的安全规则。
特别要确保你不要在Silverlight项目中这样做,因为它直接下载到客户端,并且可以在没有工具问题的情况下读取,例如Reflector。
另外,请确保您不要将配置文件(Web.config.bak)备份到您的托管文件夹中,因为它们可能会被服务器端客户端。这是网站被黑客攻击的常见方式... – Koen 2010-07-01 11:31:35
IIS不会以纯文本格式提供您提到的文件。 (除非你专门配置它做到这一点。)
所以只要IIS使用的文件是安全的,你的秘密也是安全的。 (并且存储在.cs文件中的秘密是安全的,但显然更加复杂,使用配置文件 - 所以我更喜欢使用配置文件)
但我会推荐Encrypting Configuration Information Using Protected Configuration无论如何。
@Chris,我自己有这个问题约3年。每个人都告诉我加密web.config,并保留敏感信息,但没有看到任何理由保持web.config加密。所以你的问题也可能对我有用。 – eugeneK 2010-07-01 11:48:30