public void Dispose() { _AuthToken = null; _Claims = null; _CurrentSecretKey = null; _Envelope = null; _RawToken = null; }
private void ExtractClaimsInfo() { string claimsText = _RawToken.Claims; try { _Claims = SOSCodecs.Deserialize(claimsText, typeof(JWTClaims)) as JWTClaims; } catch (Exception ex) { throw new SerializationException(string.Format("Failed To Deserialize Base 64 encoded JWT Claims to JSON Object. Text:{0}", claimsText), ex); } }
public async Task <JWTClaims> ValidateToken(Token token, AppSettings appSettings) { var tokenHandler = new JwtSecurityTokenHandler(); var jwtToken = tokenHandler.ReadToken(token.TokenId) as JwtSecurityToken; if (jwtToken == null) { return(new JWTClaims()); } var symmetricKey = Encoding.ASCII.GetBytes(appSettings.Secret); var validationParameters = new TokenValidationParameters() { RequireExpirationTime = true, ValidateIssuer = false, ValidateAudience = false, IssuerSigningKey = new SymmetricSecurityKey(symmetricKey) }; SecurityToken securityToken; var principal = tokenHandler.ValidateToken(token.TokenId, validationParameters, out securityToken); var claimList = principal.Claims.ToList(); JWTClaims claims = new JWTClaims(); Guid customerId = Guid.Parse(claimList.Where(c => c.Properties.Values.Contains("nameid")) .Select(c => c.Value) .FirstOrDefault()); Customer customer = await ctx.Customer.Where(c => c.CustomerId == customerId) .Include(c => c.Account) .Include(c => c.Account.Role) .FirstOrDefaultAsync(); claims.AccountId = customer.AccountId.ToString(); claims.CustomerId = customer.CustomerId.ToString(); claims.Name = customer.Name; claims.Email = customer.Email; claims.Role = customer.Account.Role.Name; claims.Phone = customer.Phone; claims.Address = customer.Address; claims.Birthday = customer.Birthday; //for(int i = 0; i < 8; i++) //{ // string type = claimList[i].Type; // type = type.Substring(type.IndexOf("claims/") + 7); // switch (type) // { // case "nameidentifier": // claims.CustomerId = claimList[i].Value; // break; // case "name": // claims.Name = claimList[i].Value; // break; // case "emailaddress": // claims.Email = claimList[i].Value; // break; // case "streetaddress": // claims.Address = claimList[i].Value; // break; // case "mobilephone": // claims.Phone = claimList[i].Value; // break; // case "dateofbirth": // claims.Birthday = DateTime.Parse(claimList[i].Value); // break; // case "role": // claims.Role = claimList[i].Value; // break; // case "primarysid": // claims.AccountId = claimList[i].Value; // break; // default: // claims.Phone = claimList[i].Value; // break; // } //} return(claims); }