コード例 #1
0
        /// <summary>
        /// 根据token获取个人信息
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        protected UserEntity GetUserByToken(string token)
        {
            if (!string.IsNullOrWhiteSpace(token))
            {
                UserBLL         userBLL         = new UserBLL();
                UserTokenBLL    userTokenBLL    = new UserTokenBLL();
                UserTokenEntity userTokenEntity = userTokenBLL.GetByToken(token);

                UserEntity userEntity = userBLL.GetById(userTokenEntity.userId);

                return(userEntity);
            }
            else
            {
                return(null);
            }
        }
コード例 #2
0
        /// <summary>
        /// 重写,登录过滤
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //获取跳过验证的标签如果有则跳过验证
            var isDefined = false;
            var controllerActionDescriptor = filterContext.ActionDescriptor as ControllerActionDescriptor;

            if (controllerActionDescriptor != null)
            {
                isDefined = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true)
                            .Any(a => a.GetType().Equals(typeof(SkipCheckLoginAttribute)));
            }

            if (isDefined)
            {
                return;
            }

            DataResult dr = new DataResult()
            {
                code = "300",
                msg  = "参数是必需的"
            };
            //dynamic rpas = filterContext.ActionArguments.First().Value as dynamic;

            var para = filterContext.HttpContext.Request.Form; //rpas["token"];
            //if (rpas == null)
            //{

            //    filterContext.Result = new JsonResult(dr);
            //    return;
            //}
            string token = para["token"];

            if (string.IsNullOrWhiteSpace(token))
            {
                dr.msg = "token为空!";
                filterContext.Result = new JsonResult(dr);
                return;
            }

            UserTokenBLL    userTokenBLL    = new UserTokenBLL();
            UserTokenEntity userTokenEntity = userTokenBLL.GetByToken(token);

            if (userTokenEntity == null)
            {
                dr.msg = "token错误!";
                filterContext.Result = new JsonResult(dr);
                return;
            }

            int difference = 10000;

            if (DateTime.Now.Subtract(userTokenEntity.createDate).Minutes > difference)
            {
                dr.msg = "token过期!";
                filterContext.Result = new JsonResult(dr);
                return;
            }

            userTokenBLL.UpdateTime(userTokenEntity);

            base.OnActionExecuting(filterContext);
        }