Exemplo n.º 1
0
        /// <summary>
        /// 请求header里传递参数token
        /// </summary>
        /// <param name="bindingContext"></param>
        /// <returns></returns>
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            //参数必须包含token
            if (!(bindingContext.ActionContext.HttpContext.Request.Headers.ContainsKey("token")))
            {
                return(Task.CompletedTask);
            }
            var token = bindingContext.ActionContext.HttpContext.Request.Headers["token"];
            //  解析token
            TokenModel result;

            try
            {
                result = _tokenService.ReadToken(token);
            }
            catch (Exception e)
            {
                return(Task.CompletedTask);
            }

            #region 如果后台禁用用户,需要更新模型绑定相关字段
            var account = _accountService.GetAccount(result.UserName);
            if (account == null)
            {
                return(Task.CompletedTask);
            }
            result.ID    = account.ID;
            result.IsUse = account.IsUse;
            #endregion
            bindingContext.Result = ModelBindingResult.Success(result);
            return(Task.CompletedTask);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 请求header里传递参数token
        /// </summary>
        /// <param name="bindingContext"></param>
        /// <returns></returns>
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            //参数必须包含token
            if (!(bindingContext.ActionContext.HttpContext.Request.Headers.ContainsKey("token")))
            {
                return(Task.CompletedTask);
            }
            var token = bindingContext.ActionContext.HttpContext.Request.Headers["token"];
            //  解析token
            TokenModel result;

            try
            {
                result = _tokenService.ReadToken(token);
            }
            catch (Exception e)
            {
                return(Task.CompletedTask);
            }
            var validateTo = result.ValidateTo;

            #region 如果后台禁用用户,需要更新模型绑定相关字段
            // TODO
            var account = _userService.GetAccount(result.DomainAccount);
            if (account == null)
            {
                return(Task.CompletedTask);
            }
            result            = Mapper.Map <TokenModel>(account);
            result.ValidateTo = validateTo;

            #endregion

            result.IPAddress      = bindingContext.ActionContext.HttpContext.GetClientUserIp();
            bindingContext.Result = ModelBindingResult.Success(result);
            return(Task.CompletedTask);
        }