【问题标题】:MVC 3 where to encrypt the user's password?MVC 3 在哪里加密用户的密码?
【发布时间】:2012-08-14 04:33:19
【问题描述】:

我有自己的密码加密 dll,用于在用户登录时检查用户密码,这在我的用户实体中被引用。

现在我已经创建了用户注册的功能,除了密码尚未加密之外,它工作正常。

我的问题很简单,我应该把新用户密码的加密放在哪里?我不确定,因为我知道用户的密码不应该以纯文本形式传输,因此我不知道调用加密函数的最佳位置:

  • 用户实体(其中加密 dll 已用于验证)。
  • 保存用户方法所在的用户存储库。
  • 控制用户创建视图的用户控制器。
  • 其他我没有考虑过的地方!

非常感谢

【问题讨论】:

    标签: asp.net-mvc-3 entity-framework encryption


    【解决方案1】:

    首先,对于客户端-服务器通信,我建议您使用 SSL 来避免以纯文本格式传输的敏感信息(如密码)。

    之后,通常的做法是不在任何地方保存密码(即使是加密的,而是它们的哈希值。

    你可以把哈希函数放到密码属性的set方法中。这是一个例子:

    public class Member
    {
        private string _username;
    
        public string Username
        {
            get { return _username; }
            set { _username = value.ToLowerInvariant(); }
        }
    
        public string Passhash {get;set;}
    
        public void SetPassword(string password)
        {
            Passhash = Crypto.Hash(password);
        }
    
        public bool CheckPassword(string password)
        {
            return string.Equals(Passhash, Crypto.Hash(password));
        }
    }
    
    public static class Crypto
    {
        public static string Hash(string value)
        {
            return Convert.ToBase64String(
                System.Security.Cryptography.SHA256.Create()
                .ComputeHash(Encoding.UTF8.GetBytes(value)));
        }
    }
    

    编辑:

    正如 Craig Stuntz 所指出的,此示例中的哈希代码非常简单。有关更安全的密码哈希方法,请参阅以下帖子:Hashing passwords with MD5 or sha-256 C#

    【讨论】:

    • 非常感谢,我喜欢这个答案,但是我使用的是实体框架,一旦我将密码属性更改为密码类而不是字符串,它就不会从中获取数据数据库,这是有道理的。你知道如何解决这个问题吗?
    • 非常感谢。再做一些调整,一切都按预期工作!
    • 如果您使用的是未加盐的 SHA,请不要自欺欺人地认为您比存储明文要好得多。使用现成的基于 bcrypt 的系统。
    【解决方案2】:

    在一个服务层方法中负责做两件事:

    1. 调用您的加密层以散列密码(不加密)
    2. 调用您的用户存储库以使用散列密码将用户实体持久保存到数据库中

    控制器动作当然会与服务层对话。

    【讨论】:

      【解决方案3】:

      不要自己做密码散列,甚至不要考虑加密密码。

      确保此安全的努力是巨大的。使用基于公开可用规范和算法的现有方法。

      【讨论】:

        【解决方案4】:
        //ENCODE
        
        public string base64Encode(string sData)
        {
        try
        {
        byte[] encData_byte = new byte[sData.Length];
        
        encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);
        
        string encodedData = Convert.ToBase64String(encData_byte);
        
        return encodedData;
        
        }
        catch(Exception ex)
        {
        throw new Exception("Error in base64Encode" + ex.Message);
        }
        }
        
        //DECODE
        
        public string base64Decode(string sData)
            {
                try
                {
                    System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
        
                    System.Text.Decoder utf8Decode = encoder.GetDecoder();
        
                    byte[] todecode_byte = Convert.FromBase64String(sData);
        
                    int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        
                    char[] decoded_char = new char[charCount];
        
                    utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        
                    string result = new String(decoded_char);
        
                    return result;
                }
                catch (Exception ex)
                {
                    throw new Exception("Error in base64Decode" + ex.Message);
                }
            }
        
        How to call 
        
        string encode= base64Encode(val);
        
        string decode= base64Decode(val);
        
        
        This is very helpful to decode and encode your string(password)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-05
          • 1970-01-01
          • 2011-04-30
          • 1970-01-01
          • 1970-01-01
          • 2016-08-31
          相关资源
          最近更新 更多