Esempio n. 1
0
        // 解析jwttoken
        public ClaimsPrincipal CanRefresh(string token)
        {
            ClaimsPrincipal claimsPrincipal;

            // 解析旧token
            try
            {
                claimsPrincipal = new JwtSecurityTokenHandler().ValidateToken(token, new TokenValidationParameters
                {
                    ValidIssuer              = _jwtOption.Issuer,
                    ValidateIssuer           = true,
                    ValidAudience            = _jwtOption.Audience,
                    ValidateAudience         = true,
                    IssuerSigningKey         = _jwtOption.SecurityKey,
                    ValidateIssuerSigningKey = true,
                    ValidateLifetime         = false,
                }, out SecurityToken securityToken);
            }
            catch
            {
                // token无法解析
                return(null);
            }

            // 取得旧token过期时间
            var oldExpires     = claimsPrincipal.GetClaimValue(CustomClaimTypes.Expires);
            var expireDateTime = Utils.DateTimeUtil.GetDateTime(oldExpires);

            //// 当超过刷新间或者token仍在有效期内的情况下不刷新token
            //if (expireDateTime.AddMinutes(_jwtOption.RefreshEffectiveTime) < DateTime.Now || expireDateTime > DateTime.Now)
            // 期限超过则不能刷新
            if (expireDateTime.AddMinutes(_jwtOption.RefreshEffectiveTime) < DateTime.Now)
            {
                return(null);
            }

            return(claimsPrincipal);
        }