Ejemplo n.º 1
0
        /// <summary>
        ///     Called by the ASP.NET MVC framework before the action method executes.
        /// </summary>
        /// <param name="filterContext">The filter context.</param>
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            AuditAttribute auditAttribute = filterContext.ActionDescriptor.GetCustomAttributes(true).OfType <AuditAttribute>().FirstOrDefault();

            if (auditAttribute == null)
            {
                return;
            }

            AuditingLevel level         = auditAttribute.Level;
            int           levelModifier = 0;

            //int levelModifier = this.config.Get<int>(ConfigKeys.Portal.AuditingLevelModifier, 0);
            if (levelModifier >= 0) //-1 means audit disabled completely
            {
                HttpRequestBase request = filterContext.HttpContext.Request;
                Audit           audit   = new Audit()
                {
                    AuditID      = Guid.NewGuid(),
                    SessionID    = this.GetSessionId(request),
                    UserName     = (request.IsAuthenticated) ? filterContext.HttpContext.User.Identity.Name : "Anonymous",
                    IPAddress    = request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? request.UserHostAddress,
                    AreaAccessed = request.RawUrl,
                    Timestamp    = DateTime.UtcNow,
                    Data         = this.SerializeRequest(request, (uint)levelModifier, level)
                };
                AuditingModes mode = AuditingModes.DatabaseAndLogger;

                this.StoreAuditData(audit, mode, level);
            }
        }
Ejemplo n.º 2
0
        public Task <HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func <Task <HttpResponseMessage> > continuation)
        {
            AuditAttribute auditAttribute = actionContext.ActionDescriptor.GetCustomAttributes <AuditAttribute>().FirstOrDefault();

            if (auditAttribute == null)
            {
                return(continuation());
            }

            AuditingLevel level         = auditAttribute.Level;
            int           levelModifier = 0;

            if (levelModifier >= 0) //-1 means audit disabled completely
            {
                Audit audit = new Audit()
                {
                    AuditID      = Guid.NewGuid(),
                    SessionID    = "",
                    UserName     = (actionContext.RequestContext.Principal.Identity.IsAuthenticated) ? actionContext.RequestContext.Principal.Identity.Name : "Anonymous",
                    IPAddress    = actionContext.Request.GetClientIp(),
                    AreaAccessed = actionContext.Request.RequestUri.ToString(),
                    Timestamp    = DateTimeOffset.UtcNow,
                    Data         = ""
                };
                AuditingModes mode = AuditingModes.DatabaseAndLogger;

                this.StoreAuditData(audit, mode, level);
            }

            return(continuation());
        }