Skip to content

Instantly share code, notes, and snippets.

@vaygeth89
Last active November 18, 2020 22:20
Show Gist options
  • Save vaygeth89/b16667be03dc5216a4341f13697959b5 to your computer and use it in GitHub Desktop.
Save vaygeth89/b16667be03dc5216a4341f13697959b5 to your computer and use it in GitHub Desktop.
tutorial-dotnet-JWTProtectedAPI GetMyProfile()
//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