Пример #1
0
        /// <summary>
        /// 在请求执行完后 记录请求的数据以及返回数据
        /// </summary>
        /// <param name="actionExecutedContext"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
        {
            object beginTime = null;

            if (actionExecutedContext.Request.Properties.TryGetValue(key, out beginTime))
            {
                DateTime          time      = DateTime.FromBinary(Convert.ToInt64(beginTime));
                HttpRequest       request   = HttpContext.Current.Request;
                UAParserUserAgent userAgent = new UAParserUserAgent((HttpContextBase)actionExecutedContext.Request.Properties["MS_HttpContext"]);
                DateTime          nowTime   = DateTime.Now;

                //VisitLog apiActionLog = new VisitLog
                //{
                //    //VisitId = Guid.NewGuid(),
                //    //获取action名称
                //    ActionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName,
                //    //获取Controller 名称
                //    ControllerName = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName,
                //    //获取action开始执行的时间
                //    BeginTime = time,
                //    EndTime = nowTime,
                //    //获取执行action的耗时
                //    CostTime = (nowTime - time).TotalMilliseconds,
                //    UsersAgents = request.UserAgent,
                //    //获取用户
                //    UserId = getUserID(),
                //    //获取访问的ip
                //    IPAddress = request.UserHostAddress,
                //    UserHostName = request.UserHostName,
                //    UrlReferrer = request.UrlReferrer != null ? request.UrlReferrer.AbsoluteUri : "",
                //    //获取request提交的参数
                //    RequestParamaters = GetRequestValues(actionExecutedContext),
                //    //获取response响应的结果
                //    ResponseResult = GetResponseValues(actionExecutedContext),
                //    RequestUri = request.Url.AbsoluteUri,

                //};
                using (var db = new AspodesDB())
                {
                    VisitLog apiActionLog = new VisitLog();
                    //获取action名称
                    apiActionLog.ActionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName;
                    //获取Controller 名称
                    apiActionLog.ControllerName = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
                    //获取action开始执行的时间
                    apiActionLog.BeginTime = time;
                    apiActionLog.EndTime   = nowTime;
                    //获取执行action的耗时
                    apiActionLog.CostTime    = Math.Round((nowTime - time).TotalMilliseconds, 2);
                    apiActionLog.UsersAgents = request.UserAgent;
                    //获取用户
                    apiActionLog.UserId = getUserID();
                    //获取访问的ip
                    apiActionLog.IPAddress    = request.UserHostAddress;
                    apiActionLog.UserHostName = request.UserHostName;
                    apiActionLog.UrlReferrer  = request.UrlReferrer != null ? request.UrlReferrer.AbsoluteUri : "";
                    //获取request提交的参数
                    apiActionLog.RequestParamaters = GetRequestValues(actionExecutedContext);
                    //获取response响应的结果
                    apiActionLog.ResponseResult = GetResponseValues(actionExecutedContext);
                    apiActionLog.RequestUri     = request.Url.AbsoluteUri;
                    apiActionLog.UserDevice     = userAgent.Device.ToString().Trim();
                    apiActionLog.UserBrowser    = userAgent.UserAgent.ToString().Trim();
                    apiActionLog.UserOS         = userAgent.OS.ToString().Trim();

                    if (userAgent.IsMobileDevice)
                    {
                        apiActionLog.UserPartform = "手机";
                    }

                    else if (userAgent.IsTablet)
                    {
                        apiActionLog.UserPartform = "平板";
                    }
                    else
                    {
                        apiActionLog.UserPartform = "普通电脑";
                    }


                    db.VisitLogs.Add(apiActionLog);
                    db.SaveChanges();
                }
            }
            return(base.OnActionExecutedAsync(actionExecutedContext, cancellationToken));
        }
Пример #2
0
        public JsonResult Log(string userName, string pwd, string code)
        {
            var apiResult = new APIResult();

            try {
                if (Session[vCode.SessionName] == null)
                {
                    throw new OperationExceptionFacade("验证码已过期");
                }
                else if (code.ToLower() != Session[vCode.SessionName].ToString().ToLower())
                {
                    throw new OperationExceptionFacade("验证码错误");
                }
                if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(pwd))
                {
                    throw new OperationExceptionFacade("用户名或密码不可为空");
                }

                UAParserUserAgent userAgent = new UAParserUserAgent(HttpContext);
                int deviceType = 1;
                if (userAgent.IsMobileDevice)
                {
                    deviceType = (int)DeviceTypeEnum.手机;
                }
                else if (userAgent.IsTablet)
                {
                    deviceType = (int)DeviceTypeEnum.平板;
                }
                else
                {
                    deviceType = (int)DeviceTypeEnum.电脑;
                }
                var user = UserBll.Login(userName, pwd);
                if (user == null)
                {
                    throw new OperationExceptionFacade("用户名或密码错误");
                }

                string token = string.Empty;
                if (user != null)
                {
                    token = UserTokenBL.GetNewTokenAndToCache(UserTokenType.WebToken, user.Id.ToString(), "", 00001, "", 600);
                    SetTokenToCookies(token);
                    //更新权限
                    ContainerHelper.ResolvePerHttpRequest <IPermissionBL>().UpdateCache(user.GId);
                }

                LoginLog log = new LoginLog
                {
                    Id       = IdBuilder.NextLongID(),
                    UserId   = user.Id,
                    UserName = userName,
                    GroupId  = user.Group.Id,
                    Phone    = user.Tel,
                    Ip       = GetClientIP.GetClientIPAddress(),
                    Address  = string.Empty,

                    Device    = deviceType,
                    OS        = userAgent.OS.ToString(),
                    Agent     = userAgent.UserAgent.ToString(),
                    IsSuccess = user == null ? false : true,
                    Token     = token,
                    CDate     = TypeConvert.DateTimeToInt(DateTime.Now)
                                //SysType = 2,
                                //LogType = 1,
                };
                LoginLogBL.Add(log, false);
            }
            catch (Exception ex) {
                apiResult.Ret = -1;
                apiResult.Msg = ex.Message;
                if (!(ex is OperationExceptionFacade))
                {
                    LogFactory.GetLogger().Log(LogLevel.Error, ex);
                }
            }
            return(Json(apiResult));
        }