LINQ to Entities does not recognize method "System.String sha256 (System.String)" and this method cannot be translated into a store expression

I am creating a login page and I have saved the user details and hashed the password in the CUSTOMERS table, but I cannot send the salt and entered password which I get from the database and the user to my method

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

      

Hash method

 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();
    }

      

+3


source to share


1 answer


You have an error because Linq To Entities, so Entity Framework cannot be used to link with a function that cannot be translated to SQL. So your main methods sha256

and ToString

.Net are the main reasons.

To make it work, you must first get the user by email and then check that the user has their password hash equal to genrated.



So, you need to rewrite your code like this:

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
}

      

+1


source







All Articles