Exemplo n.º 1
0
        /// <summary>
        /// Occurs after the action method is invoked.
        /// </summary>
        public async Task AfterExecutedAsync(HttpActionExecutedContext actionExecutedContext, IContextWrapper contextWrapper, bool includeModelState, bool includeResponseBody)
        {
            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();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Occurs after the action method is invoked.
        /// </summary>
        public async Task AfterExecutedAsync(HttpActionExecutedContext actionExecutedContext, IContextWrapper contextWrapper, bool includeModelState, bool includeResponseBody, bool includeResponseHeaders)
        {
            var auditAction = contextWrapper.Get <AuditApiAction>(AuditApiHelper.AuditApiActionKey);
            var auditScope  = contextWrapper.Get <AuditScope>(AuditApiHelper.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)
                    {
                        bool ignoreValue = IsResponseExplicitlyIgnored(actionExecutedContext);
                        if (actionExecutedContext.Response.Content is ObjectContent objContent)
                        {
                            auditAction.ResponseBody = new BodyContent
                            {
                                Type   = objContent.ObjectType.Name,
                                Length = objContent.Headers?.ContentLength,
                                Value  = ignoreValue ? null : objContent.Value
                            };
                        }
                        else if (actionExecutedContext.Response.Content != null)
                        {
                            var httpContent = actionExecutedContext.Response.Content;
                            auditAction.ResponseBody = new BodyContent
                            {
                                Value = ignoreValue ? null : httpContent.ReadAsStringAsync().Result
                            };

                            if (httpContent.Headers != null)
                            {
                                auditAction.ResponseBody.Type   = httpContent.Headers.ContentType.ToString();
                                auditAction.ResponseBody.Length = httpContent.Headers.ContentLength;
                            }
                        }
                        else
                        {
                            auditAction.ResponseBody = new BodyContent();
                        }
                    }

                    if (includeResponseHeaders)
                    {
                        auditAction.ResponseHeaders = ToDictionary(actionExecutedContext.Response.Headers);
                    }
                }
                else
                {
                    auditAction.ResponseStatusCode = 500;
                    auditAction.ResponseStatus     = "Internal Server Error";
                }

                // Replace the Action field and save
                (auditScope.Event as AuditEventWebApi).Action = auditAction;
                await auditScope.DisposeAsync();
            }
        }