Program.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.Text;
  2. using Common.Global;
  3. using Database.Database;
  4. using Esim.Apis.Business;
  5. using Microsoft.AspNetCore.Authentication.JwtBearer;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.IdentityModel.Tokens;
  8. var builder = WebApplication.CreateBuilder(args);
  9. // Set global configuration for use by singletons like ConfigManager
  10. GlobalConfig.Configuration = builder.Configuration;
  11. // Add services to the container.
  12. builder.Services.AddControllersWithViews();
  13. // Add DbContext with Oracle provider
  14. var connectionString = builder.Configuration.GetSection("Connection").Value;
  15. builder.Services.AddDbContext<ModelContext>(options =>
  16. options.UseOracle(connectionString));
  17. // Add Business Services
  18. builder.Services.AddScoped<IUserBusiness, UserBusinessImpl>();
  19. builder.Services.AddScoped<IArticleBusiness, ArticleBusinessImpl>();
  20. // Configure JWT Authentication
  21. var jwtKey = builder.Configuration["Jwt:Key"] ?? "EsimLaoSecretKey12345678901234567890";
  22. var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? "EsimLao";
  23. var jwtAudience = builder.Configuration["Jwt:Audience"] ?? "EsimLaoClient";
  24. builder.Services.AddAuthentication(options =>
  25. {
  26. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  27. options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  28. })
  29. .AddJwtBearer(options =>
  30. {
  31. options.TokenValidationParameters = new TokenValidationParameters
  32. {
  33. ValidateIssuer = true,
  34. ValidateAudience = true,
  35. ValidateLifetime = true,
  36. ValidateIssuerSigningKey = true,
  37. ValidIssuer = jwtIssuer,
  38. ValidAudience = jwtAudience,
  39. IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtKey))
  40. };
  41. });
  42. // Add Swagger for API documentation
  43. builder.Services.AddEndpointsApiExplorer();
  44. builder.Services.AddSwaggerGen();
  45. var app = builder.Build();
  46. app.UseSwagger();
  47. app.UseSwaggerUI();
  48. // Configure the HTTP request pipeline.
  49. //if (app.Environment.IsDevelopment())
  50. //{
  51. // app.UseSwagger();
  52. // app.UseSwaggerUI();
  53. //}
  54. //else
  55. //{
  56. // app.UseExceptionHandler("/Home/Error");
  57. // // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  58. // app.UseHsts();
  59. //}
  60. app.UseHttpsRedirection();
  61. app.UseRouting();
  62. app.UseAuthentication();
  63. app.UseAuthorization();
  64. app.MapStaticAssets();
  65. app.MapControllerRoute(
  66. name: "default",
  67. pattern: "{controller=Home}/{action=Index}/{id?}")
  68. .WithStaticAssets();
  69. app.MapControllers();
  70. app.Run();