Ejemplo n.º 1
0
        public void PreContribute(AuditContributionContext context)
        {
            var httpContext = context.ServiceProvider.GetRequiredService <IHttpContextAccessor>().HttpContext;

            if (httpContext == null)
            {
                return;
            }
            var wapper = context.CreateWapper <AspNetCoreAuditInfoWapper>();

            wapper.CurrentUser = httpContext.User?.Identity?.Name ?? "Anonymous";
            if (wapper.HttpMethod == null)
            {
                wapper.HttpMethod = httpContext.Request.Method;
            }

            if (wapper.Url == null)
            {
                wapper.Url = BuildUrl(httpContext);
            }

            if (wapper.ClientIpAddress == null)
            {
                wapper.ClientIpAddress = GetClientIpAddress(httpContext);
            }

            if (wapper.BrowserInfo == null)
            {
                wapper.BrowserInfo = GetBrowserInfo(httpContext);
            }
        }
Ejemplo n.º 2
0
        public void PreContribute()
        {
            var serviceProvider = Substitute.For <IServiceProvider>();
            var accessor        = Substitute.For <IHttpContextAccessor>();

            accessor.HttpContext.Returns(new DefaultHttpContext());
            serviceProvider.Configure().GetService(typeof(IHttpContextAccessor)).Returns(accessor);
            var context     = new AuditContributionContext(serviceProvider, new AuditInfo());
            var contributor = new AspNetCoreAuditContributor();

            Should.NotThrow(() => contributor.PreContribute(context));
        }
Ejemplo n.º 3
0
        public void PostContribute(AuditContributionContext context)
        {
            var httpContext = context.ServiceProvider.GetRequiredService <IHttpContextAccessor>().HttpContext;

            if (httpContext == null)
            {
                return;
            }
            var wapper = context.CreateWapper <AspNetCoreAuditInfoWapper>();

            if (wapper.HttpStatusCode == default)
            {
                wapper.HttpStatusCode = httpContext.Response.StatusCode;
            }
        }