A
A
Abdumalik09992018-12-10 09:13:40
ASP.NET
Abdumalik0999, 2018-12-10 09:13:40

Adding a new subject to a JWT?

Hello. My question is how do I add new data to an existing jwt token. I am trying to add like this.

JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
            JwtSecurityToken jwtToken = (JwtSecurityToken)tokenHandler.ReadToken(token);
            byte[] key = Convert.FromBase64String(secret);
            TokenValidationParameters parametrs = new TokenValidationParameters()
            {
                RequireExpirationTime = true,
                ValidateIssuer = false,
                ValidateAudience = false,
                IssuerSigningKey = new SymmetricSecurityKey(key)

            };
            SecurityToken securityToken;
            ClaimsPrincipal principal = tokenHandler.ValidateToken(token, parametrs, out securityToken);
            
            Claim myClaim=new Claim(property, value);
            ClaimsIdentity identity = new ClaimsIdentity();
            identity.AddClaim(myClaim);

            principal.AddIdentity(identity);

but when I call new added data it can't find the type I'm adding

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2018-12-20
@den_labs

I don't understand what you have wrong.
Here is an example, see https://www.red-gate.com/simple-talk/dotnet/c-prog...
On dot.net core, data can be pulled from HttpContext.User.Identity if the token is passed in the Authorization header , like Bearer {token}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question