Exemplo n.º 1
0
 /// <summary>
 /// url解密
 /// </summary>
 /// <param name="this"></param>
 /// <returns></returns>
 public static string UrlDecode(this string @this)
 {
     if (string.IsNullOrWhiteSpace(@this))
     {
         return(string.Empty);
     }
     return(CryptogramHelper.Decrypt3DES(HttpUtility.UrlDecode(@this).Replace(" ", "+")));
 }
Exemplo n.º 2
0
        /// <summary>
        /// 将参数解密并转成对应的数据类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static T UrlDecode <T>(this string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return(default(T));
            }
            var decodeStr = CryptogramHelper.Decrypt3DES(UrlHelper.UrlDecode(source).Replace(" ", "+"));

            if (string.IsNullOrEmpty(decodeStr))
            {
                return(default(T));
            }
            try
            {
                return((T)Convert.ChangeType(decodeStr, typeof(T)));
            }
            catch (Exception)
            {
                return(default(T));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// url解密
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="this"></param>
        /// <returns></returns>
        public static T UrlDecode <T>(this string @this)
        {
            if (string.IsNullOrWhiteSpace(@this))
            {
                return(default(T));
            }
            var decodeStr = CryptogramHelper.Decrypt3DES(HttpUtility.UrlDecode(@this).Replace(" ", "+"));

            if (string.IsNullOrEmpty(decodeStr))
            {
                return(default(T));
            }
            try
            {
                return((T)Convert.ChangeType(decodeStr, typeof(T)));
            }
            catch (Exception)
            {
                return(default(T));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// jwt token解密获取用户标识
        /// </summary>
        /// <param name="token">待验证的token</param>
        /// <returns>验证后的用户id,当用户id为0或者产生异常则表示Token验证失败</returns>
        public static int JwtValidate(string token)
        {
            try
            {
                var           tokenHandler = new JwtSecurityTokenHandler();
                var           jwtToken     = tokenHandler.ReadJwtToken(token);
                SecurityToken secretToken  = null;
                var           tokenValidationParameters = new TokenValidationParameters
                {
                    // The signing key must match!
                    ValidateIssuerSigningKey = true,
                    ValidateAudience         = false,
                    ValidateIssuer           = false,
                    IssuerSigningKeys        = new List <SecurityKey> {
                        GetSecretKey()
                    },
                    TokenDecryptionKey = GetSecretKey(),

                    // Validate the token expiry
                    ValidateLifetime = true,
                };
                var value       = tokenHandler.ValidateToken(token, tokenValidationParameters, out secretToken);
                var userIdClaim = jwtToken.Claims.FirstOrDefault(p => p.Type == JwtRegisteredClaimNames.Sub);
                int userId      = 0;
                if (!string.IsNullOrEmpty(userIdClaim.Value))
                {
                    Int32.TryParse(CryptogramHelper.Decrypt3DES(userIdClaim.Value, UidKey), out userId);
                }
                return(userId);
            }
            catch (SecurityTokenValidationException ex)
            {
                Log.Write($"Token校验不通过,token:{token}", MessageType.Error, typeof(JwtHelper), ex);
                return(0);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// url解密
 /// </summary>
 /// <param name="source"></param>
 /// <returns></returns>
 public static string UrlDecode(this string source)
 {
     return(CryptogramHelper.Decrypt3DES(UrlHelper.UrlDecode(source).Replace(" ", "+")));
 }