Program.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Microsoft.Extensions.FileProviders; // 정적파일 관련
  2. using Microsoft.AspNetCore.Identity;
  3. using Microsoft.AspNetCore.Identity.UI.Services;
  4. using Microsoft.EntityFrameworkCore;
  5. using bitforum.Middleware;
  6. using bitforum.Services;
  7. var builder = WebApplication.CreateBuilder(args);
  8. builder.Services.AddTransient<IEmailSender, EmailSender>();
  9. // Add http context accessor
  10. builder.Services.AddHttpContextAccessor();
  11. /**
  12. * =======================================================================================================================================================
  13. */
  14. // DbContext 설정
  15. var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
  16. builder.Services.AddDbContext<DefaultDbContext>(options => options.UseSqlServer(connectionString));
  17. builder.Services.AddDbContext<UserContext>(options => options.UseSqlServer(connectionString));
  18. /**
  19. * =======================================================================================================================================================
  20. */
  21. // Identity 서비스 추가
  22. builder.Services.AddDefaultIdentity<IdentityUser>(options =>
  23. {
  24. options.SignIn.RequireConfirmedAccount = true; // 이메일 확인 비활성화
  25. }).AddEntityFrameworkStores<DbContext>();
  26. // Identity UI 추가
  27. builder.Services.AddRazorPages();
  28. builder.Services.Configure<IdentityOptions>(options =>
  29. {
  30. // Password settings.
  31. options.Password.RequireDigit = true;
  32. options.Password.RequireLowercase = true;
  33. options.Password.RequireNonAlphanumeric = true;
  34. options.Password.RequireUppercase = true;
  35. options.Password.RequiredLength = 6;
  36. options.Password.RequiredUniqueChars = 1;
  37. // Lockout settings.
  38. options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
  39. options.Lockout.MaxFailedAccessAttempts = 5;
  40. options.Lockout.AllowedForNewUsers = true;
  41. // User settings.
  42. options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
  43. options.User.RequireUniqueEmail = false;
  44. });
  45. builder.Services.ConfigureApplicationCookie(options =>
  46. {
  47. // Cookie settings
  48. options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
  49. options.LoginPath = "/Identity/Account/Login";
  50. options.AccessDeniedPath = "/Identity/Account/AccessDenied";
  51. options.SlidingExpiration = true;
  52. });
  53. /**
  54. * =======================================================================================================================================================
  55. */
  56. // Add services to the container.
  57. builder.Services.AddControllersWithViews();
  58. builder.Logging.ClearProviders();
  59. builder.Logging.AddConsole();
  60. builder.Logging.AddDebug();
  61. var app = builder.Build();
  62. /**
  63. * =======================================================================================================================================================
  64. */
  65. app.UseMiddleware<Common>();
  66. // Configure the HTTP request pipeline.
  67. if (!app.Environment.IsDevelopment())
  68. {
  69. app.UseExceptionHandler("/Home/Error");
  70. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  71. app.UseHsts();
  72. }
  73. /**
  74. * =======================================================================================================================================================
  75. */
  76. app.UseHttpsRedirection();
  77. // 정적 파일을 제공하는 미들웨어 추가
  78. app.UseStaticFiles();
  79. app.UseStaticFiles(new StaticFileOptions
  80. {
  81. FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "node_modules")),
  82. RequestPath = "/node_modules"
  83. });
  84. /**
  85. * =======================================================================================================================================================
  86. */
  87. // 환경변수 불러오기
  88. var env = Environment.GetEnvironmentVariable("environmentVariables");
  89. // 환경변수를 static 변수로 저장
  90. app.Use(async (context, next) =>
  91. {
  92. context.Items["env"] = env;
  93. await next();
  94. });
  95. /**
  96. * =======================================================================================================================================================
  97. */
  98. app.UseRouting();
  99. app.UseAuthentication();
  100. app.UseAuthorization();
  101. app.MapRazorPages();
  102. app.MapControllerRoute(
  103. name: "default",
  104. pattern: "{controller=Home}/{action=Index}/{id?}");
  105. app.Run();