public static string GenerateAuthToken(int userId) { string jwtKey = ConfigContex.GetJwtKey(); var SymmetricSecurityLey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)); // Signing Credentials var signingCredentials = new SigningCredentials(SymmetricSecurityLey, SecurityAlgorithms.HmacSha256); // Claims var claims = new[] { new Claim(JwtRegisteredClaimNames.Sub, userId.ToString()), }; // Create Token var token = new JwtSecurityToken( issuer: ConfigContex.GetJwtIssuer(), audience: ConfigContex.GetJwtAudience(), claims, expires: DateTime.Now.AddDays(7), signingCredentials: signingCredentials ); // Encode Token var encodeToken = new JwtSecurityTokenHandler().WriteToken(token); // Return Token return(encodeToken); }
// Returns validation parameter settings private static TokenValidationParameters GetValidationParameters() { return(new TokenValidationParameters() { ValidateLifetime = true, ValidIssuer = ConfigContex.GetJwtIssuer(), ValidAudience = ConfigContex.GetJwtIssuer(), IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(ConfigContex.GetJwtKey())) }); }