gpt4 book ai didi

asp.net-mvc - LINQ to Entities 无法识别方法 'System.String sha256(System.String)' ,并且此方法无法转换为存储表达式

转载 作者:行者123 更新时间:2023-12-04 20:08:13 26 4
gpt4 key购买 nike

我正在制作一个登录页面,我将用户的详细信息和散列密码保存在 CUSTOMERS 表中,但是我无法将从数据库和用户获得的盐和键入的密码发送到我的方法

 var UserInput = db.CUSTOMERs.Where(b => b.EMAIL == cUSTOMER.EMAIL && b.PASSWORD == sha256(b.SALT+cUSTOMER.PASSWORD).ToString()).FirstOrDefault() ;

散列法

 static string sha256(string password)
{
System.Security.Cryptography.SHA256Managed crypt = new System.Security.Cryptography.SHA256Managed();
System.Text.StringBuilder hash = new System.Text.StringBuilder();
byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(password), 0, Encoding.UTF8.GetByteCount(password));
foreach (byte theByte in crypto)
{
hash.Append(theByte.ToString("x2"));
}
return hash.ToString();
}

最佳答案

您遇到错误是因为 Linq To Entities 因此 Entity Framework 不能用于组合无法转换为 SQL 的函数。所以你的自定义方法sha256ToString.Net方法是主要原因。

要使其正常工作,您必须首先通过电子邮件获取用户,然后检查用户的密码哈希值是否等于生成的密码哈希值。

所以你需要像这样重写你的代码:

var UserInput = db.CUSTOMERs.FirstOrDefault(b => b.EMAIL == cUSTOMER.EMAIL);
if(UserInput != null && UserInput.PASSWORD == sha256(UserInput.SALT+cUSTOMER.PASSWORD))
{
// The user email and password match
}
else
{
// The user not found or the password does not match
}

关于asp.net-mvc - LINQ to Entities 无法识别方法 'System.String sha256(System.String)' ,并且此方法无法转换为存储表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43814402/

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