Program.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. builder.Services.AddScoped<IContentBusiness, ContentBusinessImpl>();
  21. // Configure JWT Authentication
  22. var jwtKey = builder.Configuration["Jwt:Key"] ?? "EsimLaoSecretKey12345678901234567890";
  23. var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? "EsimLao";
  24. var jwtAudience = builder.Configuration["Jwt:Audience"] ?? "EsimLaoClient";
  25. builder.Services.AddAuthentication(options =>
  26. {
  27. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  28. options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  29. })
  30. .AddJwtBearer(options =>
  31. {
  32. options.TokenValidationParameters = new TokenValidationParameters
  33. {
  34. ValidateIssuer = true,
  35. ValidateAudience = true,
  36. ValidateLifetime = true,
  37. ValidateIssuerSigningKey = true,
  38. ValidIssuer = jwtIssuer,
  39. ValidAudience = jwtAudience,
  40. IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtKey))
  41. };
  42. });
  43. // Add Swagger for API documentation
  44. builder.Services.AddEndpointsApiExplorer();
  45. builder.Services.AddSwaggerGen();
  46. var app = builder.Build();
  47. app.UseSwagger();
  48. app.UseSwaggerUI();
  49. // Configure the HTTP request pipeline.
  50. //if (app.Environment.IsDevelopment())
  51. //{
  52. // app.UseSwagger();
  53. // app.UseSwaggerUI();
  54. //}
  55. //else
  56. //{
  57. // app.UseExceptionHandler("/Home/Error");
  58. // // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  59. // app.UseHsts();
  60. //}
  61. app.UseHttpsRedirection();
  62. app.UseRouting();
  63. app.UseAuthentication();
  64. app.UseAuthorization();
  65. app.MapStaticAssets();
  66. app.MapControllerRoute(
  67. name: "default",
  68. pattern: "{controller=Home}/{action=Index}/{id?}")
  69. .WithStaticAssets();
  70. app.MapControllers();
  71. app.Run();