Program.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Agile.Config.Client;
  2. using AspectCore.Extensions.DependencyInjection;
  3. using Microsoft.AspNetCore.Authentication.JwtBearer;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.IdentityModel.Tokens;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Serialization;
  8. using System.Text;
  9. var builder = WebApplication.CreateBuilder(args);
  10. var client = new ConfigClient();
  11. ////注册全局配置
  12. builder.Configuration.AddAgileConfig(client);
  13. //注册切面拦截
  14. builder.Host.UseServiceProviderFactory(x => new DynamicProxyServiceProviderFactory());
  15. //注册Repository
  16. builder.Services.AddDataService(builder.Configuration, Regions.IDType.Scoped);
  17. //注册Repository事务
  18. builder.Services.AddMVCTransfer();
  19. //注册Services
  20. builder.Services.AddAppService(Regions.IDType.Scoped);
  21. builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  22. //注册阿里云
  23. builder.Services.AddAliyunService(builder.Configuration);
  24. //注册缓存服务
  25. builder.Services.AddCache(builder.Configuration, Regions.DistributedCacheType.Memory);
  26. //注册RabbitMq服务
  27. builder.Services.AddRabbitMqService(builder.Configuration, Regions.IDType.Singleton);
  28. //注册用户认证
  29. builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
  30. {
  31. options.TokenValidationParameters = new TokenValidationParameters()
  32. {
  33. ValidateIssuer = true,
  34. ValidIssuer = builder.Configuration["JWT:Issuer"],
  35. ValidateAudience = true,
  36. ValidAudience = builder.Configuration["JWT:Audience"],
  37. ValidateLifetime = true,
  38. ValidateIssuerSigningKey = true,
  39. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWT:SecretKey"]))
  40. };
  41. });
  42. //所有接口开启JWT认证
  43. builder.Services.AddAuthorization(x => x.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build());
  44. //注册日志
  45. builder.Services.AddLog4();
  46. //注册cors
  47. string specificOrigins = "all";
  48. builder.Services.AddCors(option => option.AddPolicy(specificOrigins, policy =>
  49. policy.AllowAnyHeader().AllowAnyOrigin().WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS")));
  50. builder.Services.AddEndpointsApiExplorer();
  51. ////全局Json配置
  52. builder.Services.AddControllers().AddNewtonsoftJson(options =>
  53. {
  54. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  55. options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
  56. options.SerializerSettings.ContractResolver = new DefaultContractResolver();
  57. });
  58. //注册WebApi信息
  59. builder.Services.AddSwaggerGen();
  60. var app = builder.Build();
  61. // Configure the HTTP request pipeline.
  62. //if (app.Environment.IsDevelopment())
  63. //{
  64. //注册WebApi信息
  65. app.UseSwagger();
  66. app.UseSwaggerUI();
  67. //}
  68. app.UseHttpLogging();
  69. //app.UseHttpsRedirection();
  70. app.MapControllers();
  71. //app.UseBasicAuthentication();
  72. app.UseAuthentication();//在前
  73. app.UseAuthorization();//在后
  74. app.UseCors(specificOrigins);
  75. app.Use(
  76. async (context, next) =>
  77. {
  78. context.Request.EnableBuffering();
  79. await next(context);
  80. }
  81. );
  82. app.Run();