123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using AspectCore.Extensions.DependencyInjection;
- using Agile.Config.Client;
- using Microsoft.AspNetCore.Http.Json;
- using System.Text.Json;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Serialization;
- using Microsoft.AspNetCore.Authentication.JwtBearer;
- using Microsoft.IdentityModel.Tokens;
- using System.Text;
- using Microsoft.AspNetCore.Authorization;
- 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.AddAliyunService(builder.Configuration);
- //注册缓存服务
- builder.Services.AddCache(builder.Configuration, Regions.DistributedCacheType.Redis);
- //注册用户认证
- 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().AllowAnyMethod()));
- 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();
|