1

I am looking for the easiest way to implement JSON Web Token authentication using IdentityModel.Tokens.Jwt. Here is a link to a package itself:

JSON Web Token Handler For the Microsoft .Net Framework 4.5 4.0.1

3
  • What's the specific issue you are having? Commented Nov 23, 2014 at 15:25
  • My specific issue is lack of documentation and guide that shows how to use classes from this package Commented Nov 23, 2014 at 15:29
  • +1 on the sparse doc of MS package. That said, you may find this package much easier to use/customize to your needs. The project site will give you all the details. Hth.. Commented Nov 25, 2015 at 16:25

1 Answer 1

1

This is what worked for me:

var securityKey = new InMemorySymmetricSecurityKey(Encoding.Default.GetBytes("MySecretKey"));
var header = new JwtHeader(new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature, SecurityAlgorithms.Sha256Digest));
var payload = new JwtPayload();

var claims = new List<Claim>
{
    new Claim(ClaimTypes.Email, "[email protected]"),
    ...
};

payload.AddClaims(claims);

// if you need something more complex than string/string data
payload.Add("tags", new List<string> { "admin", "user" });

var token = new JwtSecurityToken(header, payload);
var tokenString = securityTokenHandler.WriteToken(token);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.