Program.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using DopInterfacePlatform.Interface;
  2. using DopInterfacePlatform.Service;
  3. using Microsoft.EntityFrameworkCore;
  4. using Swashbuckle.AspNetCore.SwaggerGen;
  5. using WebApiTest;
  6. namespace DopInterfacePlatform
  7. {
  8. public class Program
  9. {
  10. public static void Main(string[] args)
  11. {
  12. var builder = WebApplication.CreateBuilder(args);
  13. // Add services to the container.
  14. builder.Services.AddControllers();
  15. builder.Services.AddScoped<ClientIpCheckActionFilter>(container =>
  16. {
  17. var loggerFactory = container.GetRequiredService<ILoggerFactory>();
  18. var logger = loggerFactory.CreateLogger<ClientIpCheckActionFilter>();
  19. return new ClientIpCheckActionFilter(builder.Configuration["IpWhiteList"], logger);
  20. });
  21. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  22. builder.Services.AddEndpointsApiExplorer();
  23. builder.Services.AddSwaggerGen();
  24. builder.Services.AddScoped<IJwtService, JwtService>();
  25. JwtOptions jwtOpt = builder.Configuration.GetSection("JWT").Get<JwtOptions>();
  26. builder.Services.AddJWTAuthentication(jwtOpt);
  27. builder.Services.Configure<SwaggerGenOptions>(c =>
  28. {
  29. c.AddAuthenticationHeader();
  30. });
  31. builder.Services.AddDbContext<DopInterfacePlatformContext>(opt =>
  32. opt.UseSqlServer(builder.Configuration["ConnectionStrings:todoContext"]));
  33. builder.Services.AddMvcCore(options =>
  34. {
  35. options.Filters.Add<LogFilter>();
  36. }).AddApiExplorer();
  37. var app = builder.Build();
  38. // Configure the HTTP request pipeline.
  39. app.UseSwagger();
  40. app.UseSwaggerUI();
  41. app.UseHttpsRedirection();
  42. app.UseAuthentication();//注意,一定得先启动这个
  43. app.UseAuthorization();
  44. app.UseMiddleware<IpWhiteListMiddleware>(builder.Configuration["IpWhiteList"]);
  45. app.MapControllers();
  46. app.Run();
  47. }
  48. }
  49. }