예제 #1
0
        public static string Decode(string token)
        {
            try
            {
                token = NetCryptoHelper.DecryptAes(token, NetCryptoHelper.AesKey);

                IJsonSerializer   serializer = new JsonNetSerializer();
                IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
                IDateTimeProvider provider   = new UtcDateTimeProvider();
                IJwtValidator     validator  = new JwtValidator(serializer, provider);
                IJwtDecoder       decoder    = new JwtDecoder(serializer, validator, urlEncoder);

                var data = decoder.Decode(token, Secret, true);

                return(data);
            }
            catch (TokenExpiredException ex)
            {
                throw new Exception("登陆超时");
            }
            catch (SignatureVerificationException ex)
            {
                throw new Exception("用户未登录");
            }
        }
예제 #2
0
        public static string CreateToken(Dictionary <string, object> payload)
        {
            IJwtAlgorithm     algorithm  = new HMACSHA256Algorithm();
            IJsonSerializer   serializer = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtEncoder       encoder    = new JwtEncoder(algorithm, serializer, urlEncoder);

            var token = encoder.Encode(payload, Secret);

            token = NetCryptoHelper.EncryptAes(token, NetCryptoHelper.AesKey);

            return(token);
        }