Example #1
0
        private void GetUserClaimsPrincipal(IDictionary <string, object> config)
        {
            string tokenStr = this.GetToken();

            if (!IsCanReadToken(tokenStr))
            {
                throw new CustomException(1, "无效token");
            }
            HttpContext context = RequestDataHelper.GetHttpContext();

            if (context != null)
            {
                context.User = this.ValidateToken(config, tokenStr);
            }
        }
        public override void InitParams()
        {
            base.InitParams();
            //将登录信息缓存入系统变量字典
            var context = RequestDataHelper.GetHttpContext();
            if (context.User != null)
            {
                Claim claim = context.User.FindFirst(ClaimTypes.Sid);
                if (claim != null)
                {
                    string userId = claim.Value;
                    ParamsPlugin.Set("UserId", long.Parse(userId == "" ? "0" : userId));
                }

            }
        }
Example #3
0
        /// <summary>
        /// 验证token,并获取其中的信息
        /// </summary>
        /// <param name="tokenStr"></param>
        /// <returns></returns>
        private ClaimsPrincipal ValidateToken(IDictionary <string, object> config, string tokenStr)
        {
            try
            {
                tokenStr = tokenStr.Substring(7);
                string securityKey = (string)config["SecurityKey"];
                byte[] aesKeyByte  = Encoding.UTF8.GetBytes(AppConfigurtaionHelper.Configuration.GetValue <string>("AesCrypto:Key"));
                byte[] aesIvByte   = Encoding.UTF8.GetBytes(AppConfigurtaionHelper.Configuration.GetValue <string>("AesCrypto:Iv"));
                securityKey = AesCryptoUtils.Decrypt(securityKey, aesKeyByte, aesIvByte);
                var jwtTokenHandler = new JwtSecurityTokenHandler();
                var tokenParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,

                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey)),    // 加密解密Token的密钥

                    // 是否验证发布者
                    ValidateIssuer = true,
                    // 发布者名称
                    ValidIssuer = (string)config["Issuer"],

                    // 是否验证订阅者
                    ValidateAudience = true,
                    // 订阅者名称
                    ValidAudience = (string)config["Audience"],

                    // 是否验证令牌有效期
                    ValidateLifetime = true,
                    //注意这是缓冲过期时间,总的有效时间等于这个时间加上jwt的过期时间,如果不配置,默认是5分钟
                    ClockSkew = TimeSpan.FromMinutes((int)config["ClockSkew"])
                };
                SecurityToken securityToken;
                return(jwtTokenHandler.ValidateToken(tokenStr, tokenParameters, out securityToken));
            }
            catch (SecurityTokenExpiredException e)
            {
                RequestDataHelper.GetHttpContext().Response.Headers.Add("Token-Expired", "true");
                throw new CustomException(2, "token已过期");
            }
            catch (Exception e)
            {
                throw new CustomException(1, "无效token");
            }
        }