新建JwtHandler.cs

    public class JwtHandler : AppAuthorizeHandler
    {
        public override Task HandleAsync(AuthorizationHandlerContext context)
        {
            return base.HandleAsync(context);
        }
        /// <summary>
        /// 验证管道
        /// </summary>
        /// <param name="context"></param>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public override async Task<bool> PipelineAsync(AuthorizationHandlerContext context, DefaultHttpContext httpContext)
        {
            return await Task.FromResult(true);
        }
        /// <summary>
        /// 策略验证管道(校验权限)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="httpContext"></param>
        /// <param name="requirement"></param>
        /// <returns></returns>
        public override async Task<bool> PolicyPipelineAsync(AuthorizationHandlerContext context, DefaultHttpContext httpContext, IAuthorizationRequirement requirement)
        {
            return await Task.FromResult(true);
        }
    }

新建DbContext

public class AppDbContext : DbContext
{
    public virtual DbSet<Test> Test { get; set; }
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.EnableSensitiveDataLogging();//发布之后日志参数不会变为问号
        base.OnConfiguring(optionsBuilder);
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
}

修改Program.cs

Serve.Run(args);

新建Startup.cs

    public class Startup : IAppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddJwt<JwtHandler>();
            var connectionString = App.Configuration.GetConnectionString("ConnectionString");
            services.AddDbContext<AppDbContext>(
        options => options.UseSqlServer(connectionString, o => o.UseCompatibilityLevel(120)
        ));
            services.AddDbContextAutoSaveFilter<AppDbContext>();
            services.AddCorsAccessor();
            services.AddControllersWithViews().AddUnifyResult().AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Converters.AddDateFormatString();
                options.JsonSerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
                //忽略循环引用
                options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
                options.JsonSerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
                //long 类型序列化时转 string
                options.JsonSerializerOptions.Converters.AddLongTypeConverters();
            });

            //注册缓存服务 Redis模式
            services.AddSingleton<IConnectionMultiplexer>(provider =>
            {
                var redisConnectString = App.Configuration.GetConnectionString("RedisConnectionString");
                return ConnectionMultiplexer.Connect(redisConnectString);
            });
            //注册分布式锁 Redis模式
            services.AddRedisLock(options =>
            {
                // 键名前缀
                options.InstanceName = "lock";
            });
            services.AddFileLogging(o => { o.SaveDays = 30; });
        
#if DEBUG
            // 注册Swagger生成器,定义一个和多个Swagger 文档
            services.AddSwaggerGen(options =>
            {
                var provider = App.RootServices.GetService<IApiDescriptionGroupCollectionProvider>();
                foreach (var item in provider.ApiDescriptionGroups.Items)
                {
                    if (item.GroupName != null)
                        options.SwaggerDoc(item.GroupName, null);
                }

                //// TODO:一定要返回true!
                //options.DocInclusionPredicate((docName, description) => true);

                var baseDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
                var xmlFile = System.AppDomain.CurrentDomain.FriendlyName + ".xml";
                var xmlPath = Path.Combine(baseDirectory, xmlFile);
                options.IncludeXmlComments(xmlPath);
            });

#endif
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseDefaultFiles(new DefaultFilesOptions
            {
                DefaultFileNames = new List<string> { "index.html" }
            });
            app.UseStaticFiles();
            app.EnableBuffering();

            app.UseRouting();

            app.UseCorsAccessor();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                //endpoints.Map("/", () => "index.html");
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

#if DEBUG
            //启用中间件服务生成Swagger作为JSON终结点
            app.UseSwagger();
            //启用中间件服务对swagger-ui,指定Swagger JSON终结点
            app.UseSwaggerUI(options =>
            {
                var apiDescriptionGroups = App.RootServices.GetService<IApiDescriptionGroupCollectionProvider>().ApiDescriptionGroups.Items;
                foreach (var description in apiDescriptionGroups)
                {
                    options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName);
                }
                //options.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApi");
            });

#endif
        }

    }