AspNetCore模块

1、AspNetCore模块

配置审记模块,AbpAuditingOptions增加AspNetCoreAuditLogContributor。作用?

[DependsOn(
        typeof(AbpAuditingModule), 
        typeof(AbpSecurityModule),
        typeof(AbpVirtualFileSystemModule),
        typeof(AbpUnitOfWorkModule),
        typeof(AbpHttpModule),
        typeof(AbpAuthorizationModule),
        typeof(AbpDddDomainModule), //TODO: Can we remove this?
        typeof(AbpLocalizationModule),
        typeof(AbpUiModule), //TODO: Can we remove this?
        typeof(AbpValidationModule)
        )]
    public class AbpAspNetCoreModule : AbpModule
    {
        public override void PreConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddConfiguration();
        }

        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpAuditingOptions>(options =>
            {
                options.Contributors.Add(new AspNetCoreAuditLogContributor());
            });

            AddAspNetServices(context.Services);
            context.Services.AddObjectAccessor<IApplicationBuilder>();
        }

        private static void AddAspNetServices(IServiceCollection services)
        {
            services.AddHttpContextAccessor();
        }
    }

2、审计

添加审记中间件,保存审记记录

  public async Task Invoke(HttpContext httpContext)
        {
            if (!ShouldWriteAuditLog(httpContext))
            {
                await _next(httpContext);
                return;
            }

            using (var scope = _auditingManager.BeginScope())
            {
                try
                {
                    await _next(httpContext);
                }
                finally
                {
                    await scope.SaveAsync();
                }
            }
        }

        private bool ShouldWriteAuditLog(HttpContext httpContext)
        {
            if (!Options.IsEnabled)
            {
                return false;
            }

            if (!Options.IsEnabledForAnonymousUsers && !CurrentUser.IsAuthenticated)
            {
                return false;
            }

            if (!Options.IsEnabledForGetRequests && 
                string.Equals(httpContext.Request.Method, HttpMethods.Get, StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }

            return true;
        }

 AuditLogContributor的实现AspNetCoreAuditLogContributor,AuditLogContributionContext下的AuditLogInfo的HttpMethod,Url,ClientIpAddress,BrowserInfo

3、依赖注入

IHybridServiceScopeFactory的替换实现服务

public virtual IServiceScope CreateScope()
        {
            var httpContext = HttpContextAccessor.HttpContext;
            if (httpContext == null)
            {
                return ServiceScopeFactory.CreateScope();
            }

            return new NonDisposedHttpContextServiceScope(httpContext.RequestServices);
        }

        protected class NonDisposedHttpContextServiceScope : IServiceScope
        {
            public IServiceProvider ServiceProvider { get; }

            public NonDisposedHttpContextServiceScope(IServiceProvider serviceProvider)
            {
                ServiceProvider = serviceProvider;
            }

            public void Dispose()
            {
                
            }
        }

3、异常处理

异常处理中间件

4、安全

HttpContextCurrentPrincipalAccessor;

ClaimsPrincipal Principal => _httpContextAccessor.HttpContext?.User ?? base.Principal;

5、线程

HttpContextCancellationTokenProvider

public CancellationToken Token => _httpContextAccessor.HttpContext?.RequestAborted ?? CancellationToken.None;

6、Trace,中间件AbpCorrelationIdMiddleware

7、工作单元,中间件AbpUnitOfWorkMiddleware

8、VirtualFileSystem;AspNetCoreContentOptions,IFileProvider等服务

2、AspNetCore.Mvc

AbpAspNetCoreMvcModule的配置

 

转载于:https://www.cnblogs.com/cloudsu/p/11188034.html