Created
November 18, 2020 21:55
-
-
Save vaygeth89/0f1fe9a78765755d76b3c1bd3eec314d to your computer and use it in GitHub Desktop.
tutorial-dotnet-JWTProtectedAPI
This file contains hidden or 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
//Rest of the SignUp() and GenerateJWTToken() methods | |
[HttpPost] | |
[Route("sign-in")] | |
public async Task<ActionResult> SignIn(SignInData signInData) | |
{ | |
try | |
{ | |
//Todo add your business validation here | |
//! You may want to edit catched exceptions block to handle failed scenarios | |
IdentityUser user = await ValidateUserCredentials(signInData); | |
if (user == null) | |
{ | |
return BadRequest(new | |
{ | |
Message = "Invalid Credentials or User Doesn't not exist" | |
}); | |
} | |
string JWTToken = GenerateJWTToken(user); | |
return Ok(new | |
{ | |
Token = JWTToken | |
}); | |
} | |
catch (System.Exception error) | |
{ | |
return BadRequest(new | |
{ | |
message = error.Message | |
}); | |
} | |
} | |
private async Task<IdentityUser> ValidateUserCredentials(SignInData signInData) | |
{ | |
IdentityUser user = await _userManager.FindByEmailAsync(signInData.Email); | |
if (user != null) | |
{ | |
var result = _userManager.PasswordHasher.VerifyHashedPassword(user, user.PasswordHash, signInData.Password); | |
return result == PasswordVerificationResult.Failed ? null : user; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment