我将通过 [HttpPut] 操作更新我的散列密码。这是我的模型。
[Key, DatabaseGenerated (DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string FullName { get; set; }
public string UserName { get; set; }
[NotMapped]
public string Password { get; set; }
[Required]
public byte[] PasswordHash { get; set; }
[Required]
public byte[] PasswordSalt { get; set; }
public byte UserRole { get; set; }
这是我的控制器
[HttpPut("{Id}")]
public async Task<IActionResult> PutUser([FromRoute] int Id, [FromBody] TheUser user)
{
if (Id != user.Id)
{
return BadRequest();
}
CreatePasswordHash(user.Password, out byte[] passwordHash, out byte[] passwordSalt);
_context.Entry(user).State = EntityState.Modified;
user.PasswordHash = passwordHash;
user.PasswordSalt = passwordSalt;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (_context.TheUser.Find(Id) == null)
{
return NotFound();
}
throw;
}
catch
{
return BadRequest();
}
return NoContent();
}
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));
}
}
当我运行应用程序时,我收到以下错误:
事实上,我将在不知道以前的密码的情况下重置用户的密码。但问题是两个变量“passwordHash”和“passwordSalt”没有得到值。
您创建了这两个属性Required
,因此您必须将值传递给它们。
您可以Required
轻松删除属性或将空字符串传递给这些属性,并在插入db后使用其他操作或方法更新它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句