Exemplo n.º 1
0
 private void SaveLog(ActionContext context, IActionResult result)
 {
     var userId = context.HttpContext.User.GetUserIdOrZero();
     UserActionLogDto log = new UserActionLogDto();
     log.Operation = Operation;
     log.ControllerName = "";
     log.ActionName = "";
     log.UserId = userId;
     log.Ip = context.HttpContext.IpAddress();
     log.Url = context.HttpContext.RequestPath();
     log.UrlReferrer = context.HttpContext.UrlReferrer();
     log.UserAgent = context.HttpContext.UserAgent().Substr(0, 250);
     log.Method = context.HttpContext.Request.Method;
     log.QueryString = context.HttpContext.Request.QueryString.ToString().Substr(0, 250);
     if (result is JsonResult r1)
     {
         log.ActionResult = r1.Value.JsonSerialize().SubstringByByte(480);
     }
     else if (result is FileContentResult file)
     {
         log.ActionResult = file.FileDownloadName;
     }
     log.CreateTime = DateTime.Now;
     var _dbContext = context.HttpContext.RequestServices.GetService<IDbContext>();
     var _publisher = context.HttpContext.RequestServices.GetService<IEventPublisher>();
     using (var trans = _dbContext.Database.BeginTransaction())
     {
         _publisher.Publish(log);
         trans.Commit();
     }
 }
Exemplo n.º 2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                throw new VinoArgNullException();
            }

            //图片验证码
#if !DEBUG
            if (Input.ImageCode.IsNullOrEmpty())
            {
                throw new VinoException("请输入验证码!");
            }
#endif
            if (!Input.ImageCode.IsNullOrEmpty())
            {
                var code = HttpContext.Session.GetString($"ImageValidateCode_login");
                HttpContext.Session.Remove("ImageValidateCode_login");
                if (!Input.ImageCode.EqualOrdinalIgnoreCase(code))
                {
                    throw new VinoException(1, "验证码出错!");
                }
            }
            var user = await _userService.LoginAsync(Input.Account, Input.Password);

            if (user == null)
            {
                throw new VinoException("登陆出错!");
            }
            UserActionLogDto log = new UserActionLogDto();
            log.Operation      = "用户登陆";
            log.ControllerName = "Home";
            log.ActionName     = "Login";
            log.UserId         = user.Id;
            log.Ip             = HttpContext.IpAddress();
            log.Url            = HttpContext.RequestPath();
            log.UrlReferrer    = HttpContext.UrlReferrer();
            log.UserAgent      = HttpContext.UserAgent().Substr(0, 250);
            log.Method         = HttpContext.Request.Method;
            log.QueryString    = HttpContext.Request.QueryString.ToString().Substr(0, 250);
            log.CreateTime     = DateTime.Now;

            await _eventPublisher.PublishAsync(log);

            var claims = new List <Claim>()
            {
                new Claim("Account", user.Account)
                , new Claim(ClaimTypes.Name, user.Name)
                , new Claim("HeadImage", user.HeadImage ?? "")
                , new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
            };

            var token = _jwtProvider.CreateToken(claims);

            base.Response.Cookies.Append(_jwtAuthConfig.CookieName, token, new CookieOptions
            {
                HttpOnly = true
            });

            //清除用户权限缓存
            _cacheService.Remove(string.Format(CacheKeyDefinition.UserAuthCode, user.Id));
            _cacheService.Remove(string.Format(CacheKeyDefinition.UserAuthCodeEncrypt, user.Id));

            //Cookie中保存用户信息
            base.Response.Cookies.Append("user.name", user.Name);
            base.Response.Cookies.Append("user.headimage", user.HeadImage ?? "");

            return(JsonData(true));
        }