簡體   English   中英

將機密設置存儲在.cs文件而不是Web.config中是否安全?

[英]Is it safe to store confidential settings in .cs file instead Web.config?

我想在一些Setting.cs(以及靜態類)文件中存儲一些機密值(例如數據庫連接字符串,一些密碼)。

是100%確定,IIS 7不是以純文本形式提供此文件嗎? 或者有任何已知的漏洞嗎?

兩者都不安全。

編譯源文件時,可以通過反射器或ildasm等工具輕松訪問所有字符串。

配置文件是純文本,因此任何文本編輯器都可以查看此類數據。

最佳做法是加密敏感配置部分。

不提供.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為連接字符串添加更多保護。

加密的web.config是獲得最大安全性的方法......

即使您對程序集進行模糊處理,仍可能會找到這些值。 如果您控制自己的Web服務器並且沒有其他人可以訪問,那么它們在代碼和配置中都是安全的,但是例如在共享托管環境中,域管理器自動具有訪問權限,並且您依賴於其他用戶可以訪問的安全規則。訪問它。

特別要確保你不要在Silverlight項目中這樣做,因為它可以直接下載到客戶端,並且可以通過Reflector等工具輕松讀取。

我建議不要硬編碼連接字符串。 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;
            }
        }
    }
}

IIS不會提供您在純文本中提到的文件。 (除非您專門配置它來執行此操作。)

因此,只要IIS使用的文件是安全的,您的秘密也是安全的。 (並且在.cs文件中存儲秘密是微不足道的安全,但使用配置文件顯然更復雜。 - 所以我更喜歡使用配置文件)

但我建議無論如何使用受保護的配置加密配置信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM