17

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

VSO
  • 11,546
  • 25
  • 99
  • 187
  • Possible duplicate of [Decoding and verifying JWT token using System.IdentityModel.Tokens.Jwt](https://stackoverflow.com/questions/18677837/decoding-and-verifying-jwt-token-using-system-identitymodel-tokens-jwt) – Michael Freidgeim Sep 15 '17 at 11:31

1 Answers1

56

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
vinayak bhadage
  • 576
  • 5
  • 3
  • Thank you, that answer is here: http://stackoverflow.com/questions/18677837/decoding-and-verifying-jwt-token-using-system-identitymodel-tokens-jwt But I was told not to use it and CAN'T user it because of the error that I will put in the original question. – VSO Jul 01 '15 at 13:07
  • 1
    The error is telling you exactly what you need to do to fix it, namely, add a reference to the assemblies specified. Those assemblies are included in the .NET 4.5 and 4.6 frameworks and you should be able to add a reference to them in the same way that you would add a reference to an assembly like System.Collections.dll – Rich Randall Jul 01 '15 at 22:58