using Agile.Config.Client; using AspectCore.Extensions.DependencyInjection; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.IdentityModel.Tokens; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System.Text; var builder = WebApplication.CreateBuilder(args); var client = new ConfigClient(); ////注册全局配置 builder.Configuration.AddAgileConfig(client); //注册切面拦截 builder.Host.UseServiceProviderFactory(x => new DynamicProxyServiceProviderFactory()); //注册Repository builder.Services.AddDataService(builder.Configuration, Regions.IDType.Scoped); //注册Repository事务 builder.Services.AddMVCTransfer(); //注册Services builder.Services.AddAppService(Regions.IDType.Scoped); builder.Services.AddSingleton(); //注册阿里云 builder.Services.AddAliyunService(builder.Configuration); //注册缓存服务 builder.Services.AddCache(builder.Configuration, Regions.DistributedCacheType.Memory); //注册RabbitMq服务 builder.Services.AddRabbitMqService(builder.Configuration, Regions.IDType.Singleton); //注册用户认证 builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters() { ValidateIssuer = true, ValidIssuer = builder.Configuration["JWT:Issuer"], ValidateAudience = true, ValidAudience = builder.Configuration["JWT:Audience"], ValidateLifetime = true, ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWT:SecretKey"])) }; }); //所有接口开启JWT认证 builder.Services.AddAuthorization(x => x.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()); //注册日志 builder.Services.AddLog4(); //注册cors string specificOrigins = "all"; builder.Services.AddCors(option => option.AddPolicy(specificOrigins, policy => policy.AllowAnyHeader().AllowAnyOrigin().WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS"))); builder.Services.AddEndpointsApiExplorer(); ////全局Json配置 builder.Services.AddControllers().AddNewtonsoftJson(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); //注册WebApi信息 builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. //if (app.Environment.IsDevelopment()) //{ //注册WebApi信息 app.UseSwagger(); app.UseSwaggerUI(); //} app.UseHttpLogging(); //app.UseHttpsRedirection(); app.MapControllers(); //app.UseBasicAuthentication(); app.UseAuthentication();//在前 app.UseAuthorization();//在后 app.UseCors(specificOrigins); app.Use( async (context, next) => { context.Request.EnableBuffering(); await next(context); } ); app.Run();