So you were able to generate a bearer token for your authenticated users, in your frontend you put the the token in the header for make every single request authenticated good… but what if you need to get the info of the user that is requesting the information ?
Here’s a code snipped for having just that.
if (Request.Headers.Authorization.Any())
{
var bearerToken = Request.Headers.Authorization.FirstOrDefault();
var tokenRaw = bearerToken.Replace("Bearer ", "");
var handler = new JwtSecurityTokenHandler();
var token = handler.ReadJwtToken(tokenRaw);
//Read user
var GetUserID = token.Claims.Where(x => x.Type == "ID").FirstOrDefault().Value;
}
In this short example you can go over the claims you define when you created the bearer token and request the information you need, say you need the user ID to register the request in a log, or confirm he has access to the service.
Latest posts by Starlin González (see all)
- Learning Swift - June 11, 2024
- How to read Bearer Tokens Claims in dotnetcore - August 23, 2023
- Fetching emails with MailKit - October 31, 2020