Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

580
Visualizações
Parsing JWT to get claims in C#

Our senior developer wrote the following code, as an example:

public class TokenParser 
{
    private Token token;

    public Token Parse(HttpRequestMessage r)
    {
        IOwinContext context = r.GetOwinContext();
        token = new Token();
        ParseData(context);
        return token;
    }

    private void ParseData(IOwinContext context)
    {
        token.Name= context.Authentication.User.Claims.Single(x => x.Type == ClaimTypes.Name).Value;
    }
}

(There is also a "Token.cs" class that just has a name property as string.)

Our decoded JWT payload looks like this:

{
  "iss": "https://someissuer.com/",
  "sub": "I want this string, atm I get it manually",
  "aud": "11543fdsasf23432",
  "exp": 33244323433,
  "iat": 23443223434
}

The problem I run into is that when I try to get claim by Type "sub", nothing comes up (and it's not in the list). BUT "sub" seems to be an extremely common claim.

What am I doing wrong here? Go do I get the subject ("sub") claim?

Edit: For those recommending system.IdentityModel - I get this error when trying to use it:

identityModelError

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you have the token in JWT format you can use System.IdentityModel.Tokens.Jwt.dll, v2.0.0.0 and get the subject as shown below

var jwtToken = new JwtSecurityToken(token);
    jwtToken.Subject
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda