Beispiel #1
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            _logger = context.HttpContext.RequestServices.GetRequiredService <ILogger <InvalidModelStateFilter> >();
            _logger.LogDebug("Executing audit filter");

            _auditService = context.HttpContext.RequestServices.GetRequiredService <IAuditService>();
            if (_auditService == null)
            {
                throw new MSFrameworkException("AuditService is not registered");
            }

            var configuration   = context.HttpContext.RequestServices.GetRequiredService <IConfiguration>();
            var applicationName = configuration["ApplicationName"];

            applicationName = string.IsNullOrWhiteSpace(applicationName)
                                ? Assembly.GetEntryAssembly()?.FullName
                                : applicationName;
            var path = context.ActionDescriptor.GetActionPath();
            var ua   = context.HttpContext.Request.Headers["User-Agent"].ToString();
            var ip   = context.GetClientIp();

            _auditedOperation = new AuditedOperation(applicationName, path, ip, ua);
            if (context.HttpContext.User?.Identity != null && context.HttpContext.User.Identity.IsAuthenticated &&
                context.HttpContext.User.Identity is ClaimsIdentity identity)
            {
                _auditedOperation.SetCreationAudited(identity.GetUserId(), identity.GetUserName());
            }
            else
            {
                _auditedOperation.SetCreationAudited("Anonymous", "Anonymous");
            }
        }