【发布时间】:2017-11-04 09:34:45
【问题描述】:
这是我加密密码的 get/set 方法:
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password {
set {
var emp = db.Employees.Find(2);
password = EncryptDecrypt.Encrypt(emp.Password, "a15s8f5s6e2s3g1w5");
}
get {
return password;
}
}
我使用以下代码允许用户登录,我还使用它来解密密码:
private Employee slogin;
using (var db = new Entities())
{
var erg = from s in db.Employees
where s.LastName.ToString() == model.UserName && s.Password == EncryptDecrypt.Decrypt(model.Password, "a15s8f5s6e2s3g1w5")
select s;
slogin = erg.FirstOrDefault();
}
每次运行代码时,我都会在此处收到 NotSupportedException:slogin = erg.FirstOrDefault();
{"LINQ to Entities 无法识别方法'System.String Decrypt(System.String, System.String)' 方法,而这个方法不能 被翻译成商店表达式。"}
【问题讨论】:
-
您应该只通过用户名查询员工表,然后如果存在该用户名的记录,则评估密码。您正在尝试同时进行这两项操作。
-
我希望这不是针对生产站点。 ASP.NET 有自己久经考验的身份系统,不要自己发明方轮。对于初学者,密码应该是散列的,而不是加密的。