I am using asp.net core web api this is a piece of code, where i am using registered claims to add username and email in jwt token payload, now the question is.
How can I send user id in registered claims like username and email.
on line number 45 (if you see code image)
(https://i.sstatic.net/qCm42.png)
if (userdata!=null)
{
var secretkey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("secret key here");
var signingCredentials = new SigningCredentials(secretkey, SecurityAlgorithms.HmacSha256);
var claims = new[] {
new Claim(JwtRegisteredClaimNames.Name,userdata.Username),
new Claim(JwtRegisteredClaimNames.Email,userdata.Email),
new Claim(JwtRegisteredClaimNames.,userdata.Id),
};
Usually, the user ID is added as the sub claim, which stands for "subject". It identifies the subject of the token, in your case, the user. Usually, it contains the user ID, email, or login.
So, in your case:
new Claim(JwtRegisteredClaimNames.Sub, userdata.Id),
Still, I'm pretty sure you are able to use any string as the claim name. Tokens don't have to consist only of registered claims. They can contain any claims. Something like this should also work:
new Claim("my_own_user_id_claim", userdata.Id),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With