Exemple #1
0
        public void OnActionExecuting(ActionExecutingContext context)
        {
            int?   userId     = Helper.HttpContextHelper.Current.Session.GetInt32("UserId");
            string controller = ((ControllerActionDescriptor)context.ActionDescriptor).ControllerName;
            string action     = ((ControllerActionDescriptor)context.ActionDescriptor).ActionName;
            string ipAddress  = string.Empty;

            var unitOfWork = Helper.HttpContextHelper.Current.RequestServices.GetService <IUnitOfWork>();

            Data.Entities.Log log = new Data.Entities.Log()
            {
                Action     = action,
                Controller = controller,
                Active     = true,
                CreateDate = DateTime.UtcNow,
                IpAddress  = ipAddress,
                UserId     = userId
            };

            unitOfWork.LogRepository.Insert(log);
            unitOfWork.Complete();
        }
Exemple #2
0
        public async Task <IActionResult> AuthenticateUser()
        {
            if (User != null)
            {
                var username = User.Identity.Name;
                username = username.Substring(username.IndexOf(@"\") + 1);
                var userFromDb = await _context.Accounts
                                 .Include(u => u.Department)
                                 .Include(u => u.AccountRoles).ThenInclude(r => r.Role)
                                 .Where(u => u.Name == username)
                                 .FirstOrDefaultAsync();

                //var userFromDb = _userService.GetUserByName(username);
                if (userFromDb != null)
                {
                    var userToReturn = _mapper.Map <AccountForListDto>(userFromDb);
                    var newLog       = new Data.Entities.Log
                    {
                        AccountId = userFromDb.Id,
                        Action    = "login",
                        Timestamp = DateTime.Now
                    };
                    _context.Add(newLog);
                    _context.SaveChanges();
                    return(Ok(userToReturn));
                }
                else
                {
                    return(Unauthorized("Пользователь не найден в базе дынных СОРС"));
                }
            }
            else
            {
                return(Unauthorized("Ошибка Windows Authentication"));
            }
        }
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var filename = Data.Utility.MapPath(String.Format("App_Data\\{1}\\{0}\\", "Log", "Access"));

            if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(filename)))
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filename));
            }

            var files = System.IO.Directory.GetFiles(filename);

            if (files.Length > 0)
            {
                var fName = files[0];
                var name  = System.IO.Path.GetFileName(fName);
                var temp  = Data.Utility.MapPath(String.Format("App_Data\\{1}\\{0}", name, "Access"));
                if (System.IO.File.Exists(temp) == false)
                {
                    System.IO.File.Move(fName, temp);
                    var logEntity = Database.Instance().ObjectEntity <Data.Entities.Log>();
                    new System.Threading.Tasks.Task(() =>
                    {
                        try
                        {
                            using (System.IO.StreamReader reader = new System.IO.StreamReader(temp, Encoding.UTF8))
                            {
                                CSV.EachRow(reader, data =>
                                {
                                    if (data.Length > 2)
                                    {
                                        var log = new Data.Entities.Log
                                        {
                                            Time     = Utility.TimeSpan(Utility.IntParse(data[0], 0)),
                                            Username = data[1],
                                            LogKey   = data[2]
                                        };
                                        if (data.Length > 3)
                                        {
                                            log.Caption = data[3];
                                        }
                                        if (data.Length > 4)
                                        {
                                            log.IPAddress = data[4];
                                        }
                                        if (data.Length > 5)
                                        {
                                            log.UserAgent = data[5];
                                        }
                                        if (data.Length > 6)
                                        {
                                            log.Context = data[6];
                                        }
                                        logEntity.Insert(log);
                                    }
                                });
                            }
                        }
                        catch (Exception ex)
                        {
                            Utility.Error("Log", ex.ToString());
                        }
                        finally
                        {
                            System.IO.File.Delete(temp);
                        }
                    }).Start();
                    response.Redirect(new WebMeta().Put("file", name));
                }
                else
                {
                    response.Redirect(new WebMeta().Put("wait", name));
                }
            }
            else
            {
                response.Redirect(new WebMeta().Put("file", "none"));
            }
        }