예제 #1
0
        /// <summary>
        /// Occurs after the action method is invoked.
        /// </summary>
        /// <param name="actionExecutedContext">The action executed context.</param>
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            var contextWrapper = new ContextWrapper(actionExecutedContext.Request);
            var auditAction    = contextWrapper.Get <AuditApiAction>(AuditApiActionKey);
            var auditScope     = contextWrapper.Get <AuditScope>(AuditApiScopeKey);

            if (auditAction != null && auditScope != null)
            {
                auditAction.Exception        = actionExecutedContext.Exception.GetExceptionInfo();
                auditAction.ModelStateErrors = IncludeModelState ? GetModelStateErrors(actionExecutedContext.ActionContext.ModelState) : null;
                auditAction.ModelStateValid  = IncludeModelState ? actionExecutedContext.ActionContext.ModelState?.IsValid : null;
                auditAction.ResponseBodyType = actionExecutedContext.Response?.Content?.GetType().Name;
                if (actionExecutedContext.Response != null)
                {
                    auditAction.ResponseStatus     = actionExecutedContext.Response.ReasonPhrase;
                    auditAction.ResponseStatusCode = (int)actionExecutedContext.Response.StatusCode;
                    if (IncludeResponseBody)
                    {
                        var objContent = actionExecutedContext.Response.Content as ObjectContent;
                        auditAction.ResponseBody = objContent != null
                            ? new { Type = objContent.ObjectType.Name, Value = objContent.Value }
                            : (object)actionExecutedContext.Response.Content?.ReadAsStringAsync().Result;
                    }
                }
                else
                {
                    auditAction.ResponseStatusCode = 500;
                    auditAction.ResponseStatus     = "Internal Server Error";
                }
                // Replace the Action field and save
                (auditScope.Event as AuditEventWebApi).Action = auditAction;
                auditScope.Save();
            }
        }
예제 #2
0
        /// <summary>
        /// Occurs after the action method is invoked.
        /// </summary>
        /// <param name="actionExecutedContext">The action executed context.</param>
        private async Task AfterExecutedAsync(HttpActionExecutedContext actionExecutedContext)
        {
            var contextWrapper = new ContextWrapper(actionExecutedContext.Request);
            var auditAction    = contextWrapper.Get <AuditApiAction>(AuditApiActionKey);
            var auditScope     = contextWrapper.Get <AuditScope>(AuditApiScopeKey);

            if (auditAction != null && auditScope != null)
            {
                auditAction.Exception        = actionExecutedContext.Exception.GetExceptionInfo();
                auditAction.ModelStateErrors = IncludeModelState ? AuditApiHelper.GetModelStateErrors(actionExecutedContext.ActionContext.ModelState) : null;
                auditAction.ModelStateValid  = IncludeModelState ? actionExecutedContext.ActionContext.ModelState?.IsValid : null;
                if (actionExecutedContext.Response != null)
                {
                    auditAction.ResponseStatus     = actionExecutedContext.Response.ReasonPhrase;
                    auditAction.ResponseStatusCode = (int)actionExecutedContext.Response.StatusCode;
                    if (IncludeResponseBody)
                    {
                        var objContent = actionExecutedContext.Response.Content as ObjectContent;
                        auditAction.ResponseBody = new BodyContent
                        {
                            Type   = objContent != null ? objContent.ObjectType.Name : actionExecutedContext.Response.Content?.Headers?.ContentType.ToString(),
                            Length = actionExecutedContext.Response.Content?.Headers.ContentLength,
                            Value  = objContent != null ? objContent.Value : actionExecutedContext.Response.Content?.ReadAsStringAsync().Result
                        };
                    }
                }
                else
                {
                    auditAction.ResponseStatusCode = 500;
                    auditAction.ResponseStatus     = "Internal Server Error";
                }
                // Replace the Action field and save
                (auditScope.Event as AuditEventWebApi).Action = auditAction;
                await auditScope.SaveAsync();
            }
        }
예제 #3
0
        internal static AuditScope GetCurrentScope(HttpRequestMessage request)
        {
            var contextWrapper = new ContextWrapper(request);

            return(contextWrapper.Get <AuditScope>(AuditApiScopeKey));
        }