【问题标题】:LINQ to Entities does not recognize the method 'System.String sha256(System.String)' , and this method cannot be translated into a store expressionLINQ to Entities 无法识别方法 'System.String sha256(System.String)' ,并且此方法无法转换为存储表达式
【发布时间】:2017-05-05 22:06:40
【问题描述】:

我正在创建一个登录页面,并将用户的详细信息和哈希密码保存在 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();
    }

【问题讨论】:

    标签: asp.net-mvc entity-framework


    【解决方案1】:

    您有错误,因为 Linq To Entities 因此实体框架不能用于组合无法转换为 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
    }
    

    【讨论】:

    • 从 b.SALT 更改为 UserInput.SALT 非常感谢
    猜你喜欢
    • 2011-08-19
    • 2011-08-23
    • 2019-10-31
    • 2012-12-02
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    相关资源
    最近更新 更多