Esempio n. 1
0
        public static bool ValidJWT(string jwt)
        {
            JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();

            if (tokenHandler.CanReadToken(jwt))
            {
                TokenValidationParameters validationParams = new TokenValidationParameters
                {
                    RequireExpirationTime    = false,
                    RequireSignedTokens      = true,
                    ValidateIssuerSigningKey = true,
                    ValidAlgorithms          = MyJWT.algos,
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidIssuer      = MyJWT.issuer,
                    ValidAudience    = MyJWT.audience,
                    IssuerSigningKey = MyJWT.GetSymmetricSecurityKey()
                };

                try
                {
                    tokenHandler.ValidateToken(jwt, validationParams, out SecurityToken validatedToken);
                }
                catch
                {
                    return(false);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 2
0
        public static string GenerateJWT(List <Claim> listClaims)
        {
            JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();

            listClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()));

            SecurityTokenDescriptor tokenDescriptor = new SecurityTokenDescriptor
            {
                Expires            = null,
                Subject            = new ClaimsIdentity(listClaims),
                Issuer             = MyJWT.issuer,
                Audience           = MyJWT.audience,
                SigningCredentials = MyJWT.GetSigningCredentials(),
            };

            SecurityToken securityToken = tokenHandler.CreateToken(tokenDescriptor);

            return(new JwtSecurityTokenHandler().WriteToken(securityToken));
        }
Esempio n. 3
0
        public static SigningCredentials GetSigningCredentials()
        {
            SigningCredentials credentials = new SigningCredentials(MyJWT.GetSymmetricSecurityKey(), MyJWT.securityAlgorithm);

            return(credentials);
        }