gpt4 book ai didi

c# - 密码哈希不匹配

转载 作者:行者123 更新时间:2023-12-04 00:03:56 28 4
gpt4 key购买 nike

我无法登录用户,因为密码哈希不匹配。我究竟做错了什么。保存密码哈希的我的 sql 服务器具有 nvarchar 的数据类型。我之前用过二进制数据类型,但是没用。

 private readonly MovieHubContext _context;
public AuthRepository(MovieHubContext context)
{
_context = context;
}

//method is called when the user hits the login button
public async Task<Users> Login(string username, string password)
{
//returns the username from the databse
var user = await _context.Users.FirstOrDefaultAsync(x => x.UserName == username);
if (user == null)
{
return null;
}

if (!VerifyPasswordHash(password, System.Text.Encoding.UTF8.GetBytes(user.PasswordHash),
System.Text.Encoding.UTF8.GetBytes(user.PasswordSalt)))
return null;
return user;

}
// this method is used to verify the password
private bool VerifyPasswordHash(string password, byte[] passwordHash, byte[] passwordSalt)
{
using (var hmac = new System.Security.Cryptography.HMACSHA512(passwordSalt))
{
//changes the string into byte and them computes the hash
byte[] computedHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
for (int i = 0; i < computedHash.Length; i++)
{
//compares the hashed password(user input) with the hashed password from the database
if (computedHash[i] != passwordHash[i])
return false;
}
}
return true;
}
// This method is used to register the users
public async Task<Users> Register(Users users, string password)
{
byte[] passwordHash, passwordSalt;
CreatePasswordHash(password, out passwordHash, out passwordSalt);

users.PasswordHash = System.Text.Encoding.UTF8.GetString(passwordHash);
users.PasswordSalt = System.Text.Encoding.UTF8.GetString(passwordSalt);

//save into database
await _context.Users.AddAsync(users);
await _context.SaveChangesAsync();

return users;


}

private void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
{
using (var hmac = new System.Security.Cryptography.HMACSHA512())
{
passwordSalt = hmac.Key;
passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
}
}

public async Task<bool> UserExists(string username)
{
if (await _context.Users.AnyAsync(x => x.UserName == username))
return true;

return false;
}

这是我为注册和登录我的客户而编写的代码。我无法找到我做错了什么。

最佳答案

注册时只需将 UTF8.GetBytes 替换为 Convert.FromBase64StringUTF8.GetString 替换为 Convert.ToBase64String/验证

public async Task<Users> Register(Users users, string password)
{
byte[] passwordHash, passwordSalt;
CreatePasswordHash(password, out passwordHash, out passwordSalt);

users.PasswordHash = Convert.ToBase64String(passwordHash);
users.PasswordSalt = Convert.ToBase64String(passwordSalt);

//save into database
await _context.Users.AddAsync(users);
await _context.SaveChangesAsync();

return users;
}

public async Task<Users> Login(string username, string password)
{
//returns the username from the databse
var user = await _context.Users.FirstOrDefaultAsync(x => x.UserName == username);
if (user == null)
{
return null;
}

if (!VerifyPasswordHash(password, Convert.FromBase64String(user.PasswordHash),
Convert.FromBase64String(user.PasswordSalt)))
return null;
return user;

}

您的无效结果的原因是您的 GetString 正在返回一个包含不可打印字符的字符串,这些字符未正确地从/向数据库持久化/检索。

只需打印出这些字符串,看看你会得到什么。

相比之下,base64 编码可确保将字节数组转换为 安全 字符串,以便您可以安全地存储/检索它们。

这是一个稍微修改过的版本(async & dbcontext 被移除),

https://dotnetfiddle.net/fH5mXh

关于c# - 密码哈希不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52707405/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com