Answered by:
Custom WPF Authentication Problem

Question
-
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.
- Edited by gregory.bmclub Monday, January 13, 2014 3:03 PM
Monday, January 13, 2014 2:56 PM
Answers
-
You should calculate the hash before you issue the query:
public UserViewModel AuthenticateUser(string username, string clearTextPassword) { SchoolAdminDBEntities context = new SchoolAdminDBEntities(); string hashedPassword = CalculateHash(clearTextPassword, username); var userData = context.Users.Where(i => i.Username.Equals(username) && i.Password.Equals(hashedPassword)).SingleOrDefault(); if (userData == null) throw new UnauthorizedAccessException("Access denied. Please provide some valid credentials."); return new UserViewModel(userData.Username, userData.Email, userData.Role); }
- Marked as answer by gregory.bmclub Monday, January 13, 2014 3:36 PM
Monday, January 13, 2014 3:21 PM
All replies
-
Can you give a try using AsEnumerable, something similar to this below code. I don't have VisualStudio, so there may be typo errors.
var userData=(from item in context.Users where item.username.equals(username) && item.passowrd.Equals(CalculateHash(clearTextPassword, i.Username))).AsEnumerable().SingleorDefault();
Regards, http://www.shwetalodha.blogspot.in/
- Proposed as answer by Shweta Jain (Lodha) Monday, January 13, 2014 3:12 PM
Monday, January 13, 2014 3:12 PM -
You should calculate the hash before you issue the query:
public UserViewModel AuthenticateUser(string username, string clearTextPassword) { SchoolAdminDBEntities context = new SchoolAdminDBEntities(); string hashedPassword = CalculateHash(clearTextPassword, username); var userData = context.Users.Where(i => i.Username.Equals(username) && i.Password.Equals(hashedPassword)).SingleOrDefault(); if (userData == null) throw new UnauthorizedAccessException("Access denied. Please provide some valid credentials."); return new UserViewModel(userData.Username, userData.Email, userData.Role); }
- Marked as answer by gregory.bmclub Monday, January 13, 2014 3:36 PM
Monday, January 13, 2014 3:21 PM