Program.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. if (app.Environment.IsDevelopment())
  40. {
  41. app.UseSwagger();
  42. app.UseSwaggerUI();
  43. }
  44. app.UseHttpsRedirection();
  45. app.UseAuthentication();//注意,一定得先启动这个
  46. app.UseAuthorization();
  47. app.UseMiddleware<IpWhiteListMiddleware>(builder.Configuration["IpWhiteList"]);
  48. app.MapControllers();
  49. app.Run();
  50. }
  51. }
  52. }