Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
Asp.net mvc 4 在web.config文件中以不可逆加密格式存储密码_Asp.net Mvc 4_C# 4.0_Web Config_Configuration Files_Password Encryption - Fatal编程技术网

Asp.net mvc 4 在web.config文件中以不可逆加密格式存储密码

Asp.net mvc 4 在web.config文件中以不可逆加密格式存储密码,asp.net-mvc-4,c#-4.0,web-config,configuration-files,password-encryption,Asp.net Mvc 4,C# 4.0,Web Config,Configuration Files,Password Encryption,我有一个需要验证active directory帐户的MVC应用程序。我想在web.config文件中存储帐户名和密码 我要寻找的是在web.config文件中以不可逆加密格式存储此帐户密码的最佳防弹方法的建议 但是,我也欢迎采用另一种方法来满足这一要求,因为我是第一次这样做,所以我不确定其他人是如何将密码安全地存储在web.config文件中,然后从应用程序中读取密码的。您可以做以下操作: 这是加密/解密,但我认为你要求的是单向散列。如果您想要散列,这相当容易,但需要将散列存储在web.c

我有一个需要验证active directory帐户的MVC应用程序。我想在web.config文件中存储帐户名和密码

我要寻找的是在web.config文件中以不可逆加密格式存储此帐户密码的最佳防弹方法的建议


但是,我也欢迎采用另一种方法来满足这一要求,因为我是第一次这样做,所以我不确定其他人是如何将密码安全地存储在web.config文件中,然后从应用程序中读取密码的。

您可以做以下操作:

这是加密/解密,但我认为你要求的是单向散列。如果您想要散列,这相当容易,但需要将散列存储在web.config中。然后,当尝试登录时,使用预定义的算法对提交的密码进行散列,并比较是否匹配

如何进行哈希:

使用已知的salt在c#中进行散列的代码。这是很久以前从别的地方搬来的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace Helpers
{
public class SaltedHashPassword
{
    /// <summary>
    /// Hashed password to save.
    /// </summary>
    public string Hash { get; private set; }

    /// <summary>
    /// Salt used to generate the hased password. Save with the password as well.
    /// </summary>
    public string Salt { get; private set; }

    public SaltedHashPassword(string password)
    {
        var saltBytes = new byte[32];
        using (var provider = RandomNumberGenerator.Create())
        {
            provider.GetNonZeroBytes(saltBytes);
        }
        Salt = Convert.ToBase64String(saltBytes);
        var passwordAndSaltBytes = Concat(password, saltBytes);
        Hash = ComputeHash(passwordAndSaltBytes);
    }

    public static bool Verify(string salt, string hash, string password)
    {
        var saltBytes = Convert.FromBase64String(salt);
        var passwordAndSaltBytes = Concat(password, saltBytes);
        var hashAttempt = ComputeHash(passwordAndSaltBytes);
        return hash == hashAttempt;
    }

    static private string ComputeHash(byte[] bytes)
    {
        using (var sha512 = SHA512.Create())
        {
            return Convert.ToBase64String(sha512.ComputeHash(bytes));
        }
    }

    static private byte[] Concat(string password, byte[] saltBytes)
    {
        var passwordBytes = Encoding.UTF8.GetBytes(password);
        return passwordBytes.Concat(saltBytes).ToArray();
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Security.Cryptography;
命名空间帮助程序
{
公共类SaltedHashPassword
{
/// 
///要保存的哈希密码。
/// 
公共字符串哈希{get;private set;}
/// 
///Salt用于生成hased密码。也可以使用密码保存。
/// 
公共字符串Salt{get;private set;}
公共SaltedHashPassword(字符串密码)
{
var saltBytes=新字节[32];
使用(var provider=RandomNumberGenerator.Create())
{
获取非零字节(saltBytes);
}
Salt=Convert.ToBase64String(saltBytes);
var passwordAndSaltBytes=Concat(密码,saltBytes);
Hash=计算Hash(passwordAndSaltBytes);
}
公共静态bool验证(字符串salt、字符串哈希、字符串密码)
{
var saltBytes=Convert.FromBase64String(salt);
var passwordAndSaltBytes=Concat(密码,saltBytes);
var hashtrument=ComputeHash(passwordAndSaltBytes);
返回hash==hashtrunt;
}
静态私有字符串ComputeHash(字节[]字节)
{
使用(var sha512=sha512.Create())
{
返回Convert.ToBase64String(sha512.ComputeHash(字节));
}
}
静态专用字节[]Concat(字符串密码,字节[]saltBytes)
{
var passwordBytes=Encoding.UTF8.GetBytes(密码);
返回passwordBytes.Concat(saltBytes.ToArray();
}
}

}

加密不是不可逆的。密码也需要进行散列(最好是盐渍)。然后使用相同的散列算法对用户输入进行散列,并比较结果。感谢您提到散列而不是加密。在我的场景中,不会有用户输入,它只是根据AD对已知帐户进行身份验证,但管理员不能以明文形式提供密码以存储在web.config中。你认为管理员可以对密码进行哈希和加盐,然后给我哈希和加盐的密码作为密码存储在配置文件中。我不确定我是否理解这个场景,或者你为什么需要这样做,但我看没有理由不这样做。值得一读。实际上,我可能会建议使用您链接的CodeProject文章中的代码,而不是您共享的代码。不管怎样,对我来说似乎效果更好。很高兴知道,如果答案有帮助,请将其标记为选中。谢谢