Program.cs 2.4 KB

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