Skip to content

Latest commit

 

History

History
32 lines (32 loc) · 1.12 KB

File metadata and controls

32 lines (32 loc) · 1.12 KB

Auth Services

Using this Library


JSON Web Token (JWT) Service

  1. Ensure JWTService.UpdateSecretKey is pulling the SecretKey from the correct location, it defaults to looking in App.config:
    secretKey = ConfigurationManager.AppSettings.Get("SecretKey");
  2. Create an instance of JWTService
    JWTService jwtService = new JWTService();
  3. Generate a JWT
    string token = jwtService.GenerateToken(new Claim[] { new Claim(ClaimTypes.Email, "name@gmail.com") });
  4. Validate a JWT
    bool isValid = jwtService.IsTokenValid(token);
  5. Get the claims in a JWT
    // Get the tokens
    var jwtClaims = jwtService.GetTokenClaims(token);
    // Get a specific value
    string email = "";
    email = jwtClaims.Where(x => x.Type.Equals(ClaimTypes.Email.ToString())).FirstOrDefault().Value;
    // Iterate through the tokens
    long expiration= 0;
    foreach(Claim claim in jwtClaims)
    {
        if(claim.Type.Equals("exp"))
            expiration = long.Parse(claim.Value);
        else if (claim.Type.Equals(ClaimTypes.Email.ToString()))
            email = claim.Value;
    }