ABP的AspNetCore模块

1、AspNetCore模块

[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、审计

添加中间件,是否写审计日志;审计AbpAuditingOptions的配置,IsEnabled(默认为true),IsEnabledForAnonymousUsers(默认为true),当前用户是否授权,IsEnabledForGetRequests(默认false)

 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的配置

 

上一篇:C# 常用公共方法


下一篇:C#写入错误日志