I'm creating an application that requires some sort of authentication to ensure data is secured within a SQL database.
I have managed to find an example of authentication within a WPF application online, shown here; WPF Authentication
However, I have become stuck. I have managed to implement something similar but unfortunately I have come stuck when trying to incorporate a Hash method that calculated the password to convert it to enable a user to login, shown below;
private string CalculateHash(string clearTextPassword, string salt) { // Convert the salted password to a byte array byte[] saltedHashBytes = Encoding.UTF8.GetBytes(clearTextPassword + salt); // Use the hash algorithm to calculate the hash HashAlgorithm algorithm = new SHA256Managed(); byte[] hash = algorithm.ComputeHash(saltedHashBytes); // Return the hash as a base64 encoded string to be compared to the stored password return Convert.ToBase64String(hash); }
My method that authenticates is here;
public UserViewModel AuthenticateUser(string username, string clearTextPassword) { SchoolAdminDBEntities context = new SchoolAdminDBEntities(); var userData = context.Users.Where(i => i.Username.Equals(username) && i.Password.Equals(CalculateHash(clearTextPassword, i.Username))).SingleOrDefault(); if (userData == null) throw new UnauthorizedAccessException("Access denied. Please provide some valid credentials."); return new UserViewModel(userData.Username, userData.Email, userData.Role); }
N.B. My full source code is found here; Source Code
However, I get the following error;
(Credentials saved within database - Username: Mark; Password: Mark ("MB5PYIsbI2YzCUe34Q5ZU2VferIoI4Ttd+ydolWV0OE="))
ERROR: LINQ to Entities does not recognise the method 'System.String.CalculateHash(System.String, System.String)' method, and this method cannot be translated into a store expression.
Can anyone please help me shine some light in regards to what this means and how I can solve this issue?
Thanks in advance.