Beispiel #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();
            }
        }
        /// <summary>
        /// Occurs after the action method is invoked.
        /// </summary>
        /// <param name="context">The action executed context.</param>
        private async Task AfterExecutedAsync(ActionExecutedContext context)
        {
            var httpContext = context.HttpContext;
            var auditAction = httpContext.Items[AuditApiActionKey] as AuditApiAction;
            var auditScope  = httpContext.Items[AuditApiScopeKey] as AuditScope;

            if (auditAction != null && auditScope != null)
            {
                auditAction.Exception        = context.Exception.GetExceptionInfo();
                auditAction.ModelStateErrors = IncludeModelState ? AuditApiHelper.GetModelStateErrors(context.ModelState) : null;
                auditAction.ModelStateValid  = IncludeModelState ? context.ModelState?.IsValid : null;
                if (context.HttpContext.Response != null && context.Result != null)
                {
                    var statusCode = context.Result is ObjectResult && (context.Result as ObjectResult).StatusCode.HasValue ? (context.Result as ObjectResult).StatusCode.Value
                        : context.Result is StatusCodeResult ? (context.Result as StatusCodeResult).StatusCode : context.HttpContext.Response.StatusCode;
                    auditAction.ResponseStatusCode = statusCode;
                    auditAction.ResponseStatus     = GetStatusCodeString(auditAction.ResponseStatusCode);
                    if (IncludeResponseBody)
                    {
                        var bodyType = context.Result?.GetType().GetFullTypeName();
                        auditAction.ResponseBody = new BodyContent()
                        {
                            Type = bodyType
                        };
                        switch (context.Result?.GetType().Name)
                        {
                        case nameof(ObjectResult):
                            auditAction.ResponseBody.Value = (context.Result as ObjectResult).Value;
                            break;

                        case nameof(StatusCodeResult):
                            auditAction.ResponseBody.Value = string.Format("StatusCode ({0})", (context.Result as StatusCodeResult).StatusCode);
                            break;

                        case nameof(RedirectResult):
                            auditAction.ResponseBody.Value = string.Format("Redirect to {0}", (context.Result as RedirectResult).Url);
                            break;

                        default:
                            // TODO: Handle other result types
                            auditAction.ResponseBody.Value = string.Format("Result type: {0}", context.Result.GetType().GetFullTypeName());
                            break;
                        }
                    }
                }
                else
                {
                    auditAction.ResponseStatusCode = 500;
                    auditAction.ResponseStatus     = "Internal Server Error";
                }
                // Replace the Action field and save
                (auditScope.Event as AuditEventWebApi).Action = auditAction;
                await auditScope.SaveAsync();
            }
        }
        /// <summary>
        /// Occurs after the action method is invoked.
        /// </summary>
        /// <param name="context">The action executed context.</param>
        private async Task AfterExecutedAsync(ActionExecutedContext context)
        {
            var httpContext = context.HttpContext;
            var auditAction = httpContext.Items[AuditApiActionKey] as AuditApiAction;
            var auditScope  = httpContext.Items[AuditApiScopeKey] as AuditScope;

            if (auditAction != null && auditScope != null)
            {
                auditAction.Exception        = context.Exception.GetExceptionInfo();
                auditAction.ModelStateErrors = IncludeModelState ? AuditApiHelper.GetModelStateErrors(context.ModelState) : null;
                auditAction.ModelStateValid  = IncludeModelState ? context.ModelState?.IsValid : null;
                if (context.HttpContext.Response != null && context.Result != null)
                {
                    var statusCode = context.Result is ObjectResult && (context.Result as ObjectResult).StatusCode.HasValue ? (context.Result as ObjectResult).StatusCode.Value
                        : context.Result is StatusCodeResult ? (context.Result as StatusCodeResult).StatusCode : context.HttpContext.Response.StatusCode;
                    auditAction.ResponseStatusCode = statusCode;
                    auditAction.ResponseStatus     = GetStatusCodeString(auditAction.ResponseStatusCode);
                    if (IncludeResponseBody)
                    {
                        var bodyType = context.Result?.GetType().GetFullTypeName();
                        if (bodyType != null)
                        {
                            auditAction.ResponseBody = new BodyContent {
                                Type = bodyType
                            };
                            if (context.Result is ObjectResult or)
                            {
                                auditAction.ResponseBody.Value = or.Value;
                            }
                            else if (context.Result is StatusCodeResult sr)
                            {
                                auditAction.ResponseBody.Value = string.Format("StatusCode ({0})", sr.StatusCode);
                            }
                            else if (context.Result is RedirectResult rr)
                            {
                                auditAction.ResponseBody.Value = string.Format("Redirect to {0}", rr.Url);
                            }
                            else
                            {
                                auditAction.ResponseBody.Value = context.Result.ToString();
                            }
                        }
                    }
                }
                else
                {
                    auditAction.ResponseStatusCode = 500;
                    auditAction.ResponseStatus     = "Internal Server Error";
                }
                // Replace the Action field and save
                (auditScope.Event as AuditEventWebApi).Action = auditAction;
                await auditScope.SaveAsync();
            }
        }
Beispiel #4
0
        /// <summary>
        /// Occurs after the action method is invoked.
        /// </summary>
        internal async Task AfterExecutedAsync(ActionExecutedContext context, bool includeModelState, bool includeResponseBody, bool includeResponseHeaders)
        {
            var httpContext = context.HttpContext;
            var auditAction = httpContext.Items[AuditApiHelper.AuditApiActionKey] as AuditApiAction;
            var auditScope  = httpContext.Items[AuditApiHelper.AuditApiScopeKey] as AuditScope;

            if (auditAction != null && auditScope != null)
            {
                auditAction.Exception        = context.Exception.GetExceptionInfo();
                auditAction.ModelStateErrors = includeModelState ? AuditApiHelper.GetModelStateErrors(context.ModelState) : null;
                auditAction.ModelStateValid  = includeModelState ? context.ModelState?.IsValid : null;
                if (context.HttpContext.Response != null && context.Result != null)
                {
                    var statusCode = context.Result is ObjectResult && (context.Result as ObjectResult).StatusCode.HasValue ? (context.Result as ObjectResult).StatusCode.Value
                        : context.Result is StatusCodeResult ? (context.Result as StatusCodeResult).StatusCode : context.HttpContext.Response.StatusCode;
                    auditAction.ResponseStatusCode = statusCode;
                    auditAction.ResponseStatus     = AuditApiHelper.GetStatusCodeString(auditAction.ResponseStatusCode);
                    if (includeResponseBody)
                    {
                        var bodyType = context.Result.GetType().GetFullTypeName();
                        auditAction.ResponseBody = new BodyContent {
                            Type = bodyType, Value = GetResponseBody(context.ActionDescriptor, context.Result)
                        };
                    }

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

                // Replace the Action field
                (auditScope.Event as AuditEventWebApi).Action = auditAction;
                // Save, if action was not created by middleware
                if (!auditAction.IsMiddleware)
                {
                    await auditScope.DisposeAsync();
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Occurs after the action method is invoked.
        /// </summary>
        public async Task AfterExecutedAsync(ActionExecutedContext context, bool includeModelState, bool includeResponseBody)
        {
            var httpContext = context.HttpContext;
            var auditAction = httpContext.Items[AuditApiActionKey] as AuditApiAction;
            var auditScope  = httpContext.Items[AuditApiScopeKey] as AuditScope;

            if (auditAction != null && auditScope != null)
            {
                auditAction.Exception        = context.Exception.GetExceptionInfo();
                auditAction.ModelStateErrors = includeModelState ? AuditApiHelper.GetModelStateErrors(context.ModelState) : null;
                auditAction.ModelStateValid  = includeModelState ? context.ModelState?.IsValid : null;
                if (context.HttpContext.Response != null && context.Result != null)
                {
                    var statusCode = context.Result is ObjectResult && (context.Result as ObjectResult).StatusCode.HasValue ? (context.Result as ObjectResult).StatusCode.Value
                        : context.Result is StatusCodeResult ? (context.Result as StatusCodeResult).StatusCode : context.HttpContext.Response.StatusCode;
                    auditAction.ResponseStatusCode = statusCode;
                    auditAction.ResponseStatus     = GetStatusCodeString(auditAction.ResponseStatusCode);
                    if (includeResponseBody)
                    {
                        var bodyType = context.Result?.GetType().GetFullTypeName();
                        if (bodyType != null)
                        {
                            auditAction.ResponseBody = new BodyContent {
                                Type = bodyType, Value = GetResponseBody(context.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();
            }
        }
Beispiel #6
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();
            }
        }