Program.cs 4.7 KB

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