How to put user id in registered claims

Viewed 645

enter image description hereI 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.enter image description here How can I send user id in registered claims like username and email. on line number 45 (if you see code image) (https://i.stack.imgur.com/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),
                }; 
1 Answers

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),
Related