示例#1
0
        public static JwtModel getToken(string token)
        {
            JwtModel jwtmodel = new JwtModel();
            var      secret   = "9720cbfbb0684617a2afbe466e100ba2";

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

                var json = decoder.Decode(token, secret, verify: true);
                var ser  = new JavaScriptSerializer();
                ser.RegisterConverters(new[] { new DynamicJsonConverter() });

                dynamic result = serializer.Deserialize <object>(json);
                jwtmodel.userid      = result.id;
                jwtmodel.usercode    = result.usercode;
                jwtmodel.username    = result.username;
                jwtmodel.isadmin     = result.isadmin;
                jwtmodel.rolecode    = result.rolecode;
                jwtmodel.message     = "获取成功";
                jwtmodel.status_code = 200;
            }
            catch (TokenExpiredException)
            {
                jwtmodel.message     = "Token has expired";
                jwtmodel.status_code = 402;
            }
            catch (SignatureVerificationException)
            {
                jwtmodel.message     = "Token has invalid signature";
                jwtmodel.status_code = 402;
            }
            catch (Exception)
            {
                jwtmodel.message     = "Token has invalid signature";
                jwtmodel.status_code = 402;
            }
            return(jwtmodel);
        }