Query JWT Token Claim
How can one query the user of an Jwt token in the web server?
The Java WebToken contains so-called claims. This claim information can be requested during the exchange.
The user of a claim can be determined by a request in Asp Controller with Claims / Name
var user_in_token = HttpContext.User.Claims.Where(c => c.Type == ClaimsIdentity.DefaultNameClaimType).FirstOrDefault();
|
Result in this case:
Identity user name is: myUser
The jwtToken was created in the TokenController
public string create_UserToken(string sUsername) { //-------------< create_UserToken() >------------- Claim[] claims = new Claim[] { new Claim(ClaimTypes.Name, sUsername), //*->User.Identity.Name or "Raimund Popp" new Claim(JwtRegisteredClaimNames.Email, "raimund.popp@codedocu.de") };
//--< Create a Token >-- JwtSecurityToken jwtToken = new JwtSecurityToken( issuer: "website_freelancer", //ASP.NET Core web application audience: "webclients_freelancer", //client app claims: claims, notBefore: DateTime.Now, expires: DateTime.Now.AddDays(1), signingCredentials: new SigningCredentials(_secretKey, SecurityAlgorithms.HmacSha256) ); //--</ Create a Token >--
string stringToken = new JwtSecurityTokenHandler().WriteToken(jwtToken);
return stringToken; //-------------</ create_UserToken() >------------- }
|
Among the claims you can include many predefined information
Claim[] claims = new Claim[] { new Claim(ClaimTypes.Name, sUsername), //*->User.Identity.Name or "Raimund Popp" new Claim(ClaimTypes.UserData, "ID=123"), new Claim(JwtRegisteredClaimNames.Email, "raimund.popp@codedocu.de")
}; |
A list of the ClaimTypes can be found at
[Lnk "https://msdn.microsoft.com/de-de/library/system.security.claims.claimtypes(v=vs.110).aspx" /]
the most important claim types would be (namespace: System.Security.Claims)
Asp.Net Core 2 MVC
Asp Web Api Api
JWT Java Web Token