Last active
November 18, 2020 22:20
-
-
Save vaygeth89/b16667be03dc5216a4341f13697959b5 to your computer and use it in GitHub Desktop.
tutorial-dotnet-JWTProtectedAPI GetMyProfile()
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 SignIn() and ValidateUserCredentials() methods | |
[HttpGet] | |
//We add this annotation to tell the framework this is service/route requires Authorization Claims i.e JWT | |
[Authorize] | |
[Route("my-profile")] | |
public async Task<ActionResult<UserProfile>> GetMyProfile() | |
{ | |
try | |
{ | |
//Todo add your business validation here | |
//! You may want to edit catched exceptions block to handle failed scenarios | |
string userEmail = User.FindFirst(ClaimTypes.Email)?.Value; | |
IdentityUser user = await _userManager.FindByEmailAsync(userEmail); | |
return Ok(new UserProfile() | |
{ | |
Email = user.Email, | |
Username = user.UserName, | |
PhoneNumber = user.PhoneNumber | |
}); | |
} | |
catch (System.Exception error) | |
{ | |
return BadRequest(new | |
{ | |
message = error.Message | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment