Created
May 6, 2019 08:02
-
-
Save nishanc/67b853043bc189babed1739e3a523c25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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