Skip to content

Instantly share code, notes, and snippets.

@nishanc
Created May 6, 2019 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishanc/67b853043bc189babed1739e3a523c25 to your computer and use it in GitHub Desktop.
Save nishanc/67b853043bc189babed1739e3a523c25 to your computer and use it in GitHub Desktop.
public async Task<User> Login(string username, string password)
{
var user = await _context.Users.FirstOrDefaultAsync(x => x.Username == username); //Get user from database.
if(user == null)
return null; // User does not exist.
if(!VerifyPassword(password, user.PasswordHash,user.PasswordSalt))
return null;
return user;
}
private bool VerifyPassword(string password, byte[] passwordHash, byte[] passwordSalt)
{
using(var hmac = new System.Security.Cryptography.HMACSHA512(passwordSalt)){
var computedHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password)); // Create hash using password salt.
for (int i = 0; i < computedHash.Length; i++){ // Loop through the byte array
if(computedHash[i] != passwordHash[i]) return false; // if mismatch
}
}
return true; //if no mismatches.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment