Program.cs 2.9 KB

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