Ejemplo n.º 1
0
        private async Task AfterInvoke(HttpContext context, bool includeResponseBody, Exception exception)
        {
            var auditAction = context.Items[AuditApiHelper.AuditApiActionKey] as AuditApiAction;
            var auditScope  = context.Items[AuditApiHelper.AuditApiScopeKey] as AuditScope;

            if (auditAction != null && auditScope != null)
            {
                if (exception != null)
                {
                    auditAction.Exception          = exception.GetExceptionInfo();
                    auditAction.ResponseStatusCode = 500;
                    auditAction.ResponseStatus     = "Internal Server Error";
                }
                else if (context.Response != null)
                {
                    var statusCode = context.Response.StatusCode;
                    auditAction.ResponseStatusCode = statusCode;
                    auditAction.ResponseStatus     = AuditApiHelper.GetStatusCodeString(statusCode);
                    if (includeResponseBody && auditAction.ResponseBody == null)
                    {
                        auditAction.ResponseBody = new BodyContent
                        {
                            Type   = context.Response.ContentType,
                            Length = context.Response.ContentLength,
                            Value  = AuditApiHelper.GetResponseBody(context)
                        };
                    }
                }
                // Replace the Action field and save
                (auditScope.Event as AuditEventWebApi).Action = auditAction;
                await auditScope.SaveAsync();
            }
        }
Ejemplo n.º 2
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();
                }
            }
        }
Ejemplo n.º 3
0
        private async Task AfterInvoke(HttpContext context, bool includeResponseBody, bool includeResponseHeaders, Exception exception)
        {
#pragma warning disable IDE0019 // Use pattern matching
            var auditAction = context.Items[AuditApiHelper.AuditApiActionKey] as AuditApiAction;
            var auditScope  = context.Items[AuditApiHelper.AuditApiScopeKey] as AuditScope;
#pragma warning restore IDE0019 // Use pattern matching

            if (auditAction != null && auditScope != null)
            {
                if (exception != null)
                {
                    auditAction.Exception          = exception.GetExceptionInfo();
                    auditAction.ResponseStatusCode = 500;
                    auditAction.ResponseStatus     = "Internal Server Error";
                }
                else if (context.Response != null)
                {
                    var statusCode = context.Response.StatusCode;
                    auditAction.ResponseStatusCode = statusCode;
                    auditAction.ResponseStatus     = AuditApiHelper.GetStatusCodeString(statusCode);
                    if (includeResponseBody && auditAction.ResponseBody == null)
                    {
                        auditAction.ResponseBody = new BodyContent
                        {
                            Type   = context.Response.ContentType,
                            Length = context.Response.ContentLength,
                            Value  = await AuditApiHelper.GetResponseBody(context)
                        };
                    }
                }
                if (includeResponseHeaders)
                {
                    auditAction.ResponseHeaders = AuditApiHelper.ToDictionary(context.Response.Headers);
                }
                // Replace the Action field and save
                (auditScope.Event as AuditEventWebApi).Action = auditAction;
                await auditScope.DisposeAsync();
            }
        }