Beispiel #1
0
        private async Task BeforeExecutingAsync(ActionExecutingContext filterContext)
        {
            var httpContext       = filterContext.HttpContext;
            var request           = httpContext.Request;
            var actionDescriptior = filterContext.ActionDescriptor as ControllerActionDescriptor;

            var auditAction = new AuditAction()
            {
                UserName         = httpContext.User?.Identity.Name,
                IpAddress        = httpContext.Connection?.RemoteIpAddress.ToString(),
                RequestUrl       = string.Format("{0}://{1}{2}", httpContext.Request.Scheme, httpContext.Request.Host, httpContext.Request.Path),
                HttpMethod       = httpContext.Request.Method,
                FormVariables    = httpContext.Request.HasFormContentType ? ToDictionary(httpContext.Request.Form) : null,
                Headers          = IncludeHeaders ? ToDictionary(httpContext.Request.Headers) : null,
                ActionName       = actionDescriptior != null ? actionDescriptior.ActionName : actionDescriptior.DisplayName,
                ControllerName   = actionDescriptior != null ? actionDescriptior.ControllerName : null,
                ActionParameters = GetActionParameters(filterContext.ActionArguments)
            };
            var eventType = (EventTypeName ?? "{verb} {controller}/{action}").Replace("{verb}", auditAction.HttpMethod)
                            .Replace("{controller}", auditAction.ControllerName)
                            .Replace("{action}", auditAction.ActionName);
            // Create the audit scope
            var auditEventAction = new AuditEventMvcAction()
            {
                Action = auditAction
            };
            var auditScope = await AuditScope.CreateAsync(new AuditScopeOptions()
            {
                EventType = eventType, AuditEvent = auditEventAction, CallingMethod = actionDescriptior.MethodInfo
            });

            httpContext.Items[AuditActionKey] = auditAction;
            httpContext.Items[AuditScopeKey]  = auditScope;
        }
Beispiel #2
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var request     = filterContext.HttpContext.Request;
            var auditAction = new AuditAction()
            {
                UserName         = (request.IsAuthenticated) ? filterContext.HttpContext.User?.Identity.Name : "Anonymous",
                IpAddress        = request.ServerVariables?["HTTP_X_FORWARDED_FOR"] ?? request.UserHostAddress,
                RequestUrl       = request.RawUrl,
                HttpMethod       = request.HttpMethod,
                FormVariables    = ToDictionary(request.Form),
                Headers          = IncludeHeaders ? ToDictionary(request.Headers) : null,
                ActionName       = filterContext.ActionDescriptor?.ActionName,
                ControllerName   = filterContext.ActionDescriptor?.ControllerDescriptor?.ControllerName,
                ActionParameters = filterContext.ActionParameters?.ToDictionary(k => k.Key, v => v.Value)
            };
            var eventType = (EventTypeName ?? "{verb} {controller}/{action}").Replace("{verb}", auditAction.HttpMethod)
                            .Replace("{controller}", auditAction.ControllerName)
                            .Replace("{action}", auditAction.ActionName);
            // Create the audit scope
            var auditEventAction = new AuditEventMvcAction()
            {
                Action = auditAction
            };
            var options = new AuditScopeOptions()
            {
                EventType     = eventType,
                AuditEvent    = auditEventAction,
                CallingMethod = (filterContext.ActionDescriptor as ReflectedActionDescriptor)?.MethodInfo
            };
            var auditScope = AuditScope.Create(options);

            filterContext.HttpContext.Items[AuditActionKey] = auditAction;
            filterContext.HttpContext.Items[AuditScopeKey]  = auditScope;
            base.OnActionExecuting(filterContext);
        }
Beispiel #3
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Configuration.AuditDisabled || IsActionIgnored(filterContext.ActionDescriptor))
            {
                base.OnActionExecuting(filterContext);
                return;
            }
            var request     = filterContext.HttpContext.Request;
            var auditAction = new AuditAction()
            {
                UserName  = (request.IsAuthenticated) ? filterContext.HttpContext.User?.Identity.Name : "Anonymous",
                IpAddress = request.ServerVariables?["HTTP_X_FORWARDED_FOR"] ?? request.UserHostAddress,
#if NET45
                RequestUrl    = request.Unvalidated.RawUrl,
                FormVariables = ToDictionary(request.Unvalidated.Form),
                Headers       = IncludeHeaders ? ToDictionary(request.Unvalidated.Headers) : null,
#else
                RequestUrl    = request.RawUrl,
                FormVariables = ToDictionary(request.Form),
                Headers       = IncludeHeaders ? ToDictionary(request.Headers) : null,
#endif
                RequestBody      = IncludeRequestBody ? GetRequestBody(filterContext.HttpContext) : null,
                HttpMethod       = request.HttpMethod,
                ActionName       = filterContext.ActionDescriptor?.ActionName,
                ControllerName   = filterContext.ActionDescriptor?.ControllerDescriptor?.ControllerName,
                ActionParameters = GetActionParameters(filterContext),
                TraceId          = null
            };
            var eventType = (EventTypeName ?? "{verb} {controller}/{action}").Replace("{verb}", auditAction.HttpMethod)
                            .Replace("{controller}", auditAction.ControllerName)
                            .Replace("{action}", auditAction.ActionName);
            // Create the audit scope
            var auditEventAction = new AuditEventMvcAction()
            {
                Action = auditAction
            };
            var options = new AuditScopeOptions()
            {
                EventType     = eventType,
                AuditEvent    = auditEventAction,
                CallingMethod = (filterContext.ActionDescriptor as ReflectedActionDescriptor)?.MethodInfo
            };
            var auditScope = AuditScope.Create(options);

            filterContext.HttpContext.Items[AuditActionKey] = auditAction;
            filterContext.HttpContext.Items[AuditScopeKey]  = auditScope;
            base.OnActionExecuting(filterContext);
        }
        public virtual async Task BeforeExecutingAsync(PageHandlerExecutingContext context)
        {
            var httpContext       = context.HttpContext;
            var request           = httpContext.Request;
            var actionDescriptior = context.ActionDescriptor;

            var auditAction = new AuditAction()
            {
                UserName         = httpContext.User?.Identity.Name,
                IpAddress        = httpContext.Connection?.RemoteIpAddress?.ToString(),
                RequestUrl       = string.Format("{0}://{1}{2}{3}", request.Scheme, request.Host, request.Path, request.QueryString),
                HttpMethod       = request.Method,
                FormVariables    = request.HasFormContentType ? AuditHelper.ToDictionary(request.Form) : null,
                Headers          = IncludeHeaders ? AuditHelper.ToDictionary(request.Headers) : null,
                ActionName       = actionDescriptior?.DisplayName,
                ControllerName   = actionDescriptior?.AreaName,
                ActionParameters = GetActionParameters(context),
                RequestBody      = new BodyContent {
                    Type = request.ContentType, Length = request.ContentLength, Value = IncludeRequestBody ? await GetRequestBody(context) : null
                },
                TraceId  = httpContext.TraceIdentifier,
                ViewPath = actionDescriptior?.ViewEnginePath,
                PageHandlerExecutingContext = context
            };

            var eventType = (EventTypeName ?? "{verb} {path}")
                            .Replace("{verb}", auditAction.HttpMethod)
                            .Replace("{area}", auditAction.ControllerName)
                            .Replace("{controller}", auditAction.ControllerName)
                            .Replace("{action}", auditAction.ActionName)
                            .Replace("{path}", auditAction.ViewPath);
            // Create the audit scope
            var auditEventAction = new AuditEventMvcAction()
            {
                Action = auditAction
            };
            var auditScope = await AuditScope.CreateAsync(new AuditScopeOptions()
            {
                EventType = eventType, AuditEvent = auditEventAction, CallingMethod = context.HandlerMethod?.MethodInfo
            });

            httpContext.Items[AuditActionKey] = auditAction;
            httpContext.Items[AuditScopeKey]  = auditScope;
        }