密码哈希曾经工作,现在我无法进入我的软件
我根据Code Project文章对我的登录进行了腌制和散列
当我这样做时,我在数据库中的密码和盐字段只是varchar列。 数据在SQL Server Management Studio中显示为问号。
然后一位朋友来了并将列更改为nvarchar数据类型,现在数据似乎是亚洲字母/单词的集合 – 我认为即使是那些读过任何语言的人也没有任何意义。
问题是,现在,即使我创建了新用户,每次登录尝试都会失败。
用户添加和登录
public User Login() // returns an instance of its own class { // get the salt value for the user var auser = ie.users.FirstOrDefault(u => String.Compare(u.emailaddress, emailaddress, false) == 0); if (auser == null) { throw new ValidationException("User not found"); } // hash the login for comparison to stored password hash HashGenerator h = new HashGenerator(password, auser.usersalt); string hash = h.GetHash(); var us = ie.users.FirstOrDefault(u => String.Compare(u.emailaddress, emailaddress, false) == 0 && String.Compare(u.password, password, false) == 0); if (us == null) { throw new Exception("Invalid email address and/or password."); // seems here's where it goes wrong } User user = new User(); user.userid = us.userid; user.storeid = us.storeid; user.role = us.role; return user; // login succeeded, return the data to the calling method } public void Add() { user newuser = new user(); newuser.storeid = storeid; newuser.emailaddress = emailaddress; // Generate password hash string usersalt = SaltGenerator.GetSaltString(); HashGenerator hash = new HashGenerator(password, usersalt); newuser.password = hash.GetHash(); newuser.role = role; newuser.usersalt = usersalt; ie.users.Add(newuser); ie.SaveChanges(); }
哈希和盐生成器
public class HashGenerator { public string pass { get; protected set; } public string salt { get; protected set; } public HashGenerator(string Password, string Salt) { pass = Password; salt = Salt; } public string GetHash() { SHA512 sha = new SHA512CryptoServiceProvider(); byte[] data = CryptUtility.GetBytes(String.Format("{0}{1}", pass, salt)); byte[] hash = sha.ComputeHash(data); return CryptUtility.GetString(hash); } } public static class SaltGenerator { private static RNGCryptoServiceProvider provider = null; private const int saltSize = 128; static SaltGenerator() { provider = new RNGCryptoServiceProvider(); } public static string GetSaltString() { byte[] saltBytes = new byte[saltSize]; provider.GetNonZeroBytes(saltBytes); return CryptUtility.GetString(saltBytes); } }
用于获取字符串和字节的Crypt实用程序
class CryptUtility { public static byte[] GetBytes(string Str) { byte[] bytes = new byte[Str.Length * sizeof(char)]; System.Buffer.BlockCopy(Str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; } public static string GetString(byte[] Bytes) { char[] chars = new char[Bytes.Length / sizeof(char)]; System.Buffer.BlockCopy(Bytes, 0, chars, 0, Bytes.Length); return new string(chars); } }
登录过去工作,此代码从那时起没有改变…唯一改变的是用户表中的密码和salt列现在是nvarchar
而不是varchar
。
也就是说,我想我会使用上面的add方法创建一个新用户,但是使用该用户登录也会失败。
我真的不知道如何解决这个问题。 如果我无法访问它进行测试,我无法继续开发系统的其余部分。
任何帮助将不胜感激!
你不应该使用变量
哈希
比较?
// hash the login for comparison to stored password hash HashGenerator h = new HashGenerator(password, auser.usersalt); string hash = h.GetHash(); var us = ie.users.FirstOrDefault(u => String.Compare(u.emailaddress, emailaddress, false) == 0 && String.Compare(u.password, hash, false) == 0);
进行数据库备份并转换回varchar。 或者在转换之前回滚到备份。 在将数据类型转换为nvarchar时,您的朋友似乎做错了什么。
- 使用RequireHttpsAttribute ASP.NET Core的HTTP错误310 ERR_TOO_MANY_REDIRECTS
- 使用内存映射文件的缺点
- 按文本而不是值对Datagridview的列进行排序
- 如何使用DropDownList绑定到WebGrid razor asp net mvc 4内的模型?
- C#:任何抑制编译器错误的方法都类似于抑制警告消息?
- 通过C#应用程序接收电子邮件和下载附件
- 将字节数组转换为Stream时,ReadTimeout引发exception
- 为什么总是需要在具有IDisposable成员的对象上实现IDisposable?
- 在内存中缓存应用程序数据:MVC Web API