我不知道如何为 C# 数据模型制作自定义设置器。场景很简单,我希望我的密码使用 SHA256 函数自动加密。SHA256 功能非常好用(我以前在无数项目中使用过)。
我已经尝试了几件事,但是当我运行update-database
它时,它似乎在递归地做某事并且我的 Visual Studio 挂起(不要发送错误)。请帮助我了解如何在模型中默认加密密码。
用我已经尝试过的代码
public class Administrator
{
public int ID { get; set; }
[Required]
public string Username { get; set; }
[Required]
public string Password
{
get
{
return this.Password;
}
set
{
// All this code is crashing Visual Studio
// value = Infrastructure.Encryption.SHA256(value);
// Password = Infrastructure.Encryption.SHA256(value);
// this.Password = Infrastructure.Encryption.SHA256(value);
}
}
}
种子
context.Administrators.AddOrUpdate(x => x.Username, new Administrator { Username = "admin", Password = "123" });