Program.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. }).AddEntityFrameworkStores<UserContext>().AddDefaultTokenProviders();
  28. // Identity UI 추가
  29. builder.Services.AddRazorPages();
  30. builder.Services.Configure<IdentityOptions>(options =>
  31. {
  32. // Password settings.
  33. options.Password.RequireDigit = true;
  34. options.Password.RequireLowercase = true;
  35. options.Password.RequireNonAlphanumeric = true;
  36. options.Password.RequireUppercase = true;
  37. options.Password.RequiredLength = 6;
  38. options.Password.RequiredUniqueChars = 1;
  39. // Lockout settings.
  40. options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
  41. options.Lockout.MaxFailedAccessAttempts = 5;
  42. options.Lockout.AllowedForNewUsers = true;
  43. // User settings.
  44. options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
  45. options.User.RequireUniqueEmail = false;
  46. });
  47. builder.Services.ConfigureApplicationCookie(options =>
  48. {
  49. // Cookie settings
  50. options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
  51. options.LoginPath = "/Identity/Account/Login";
  52. options.AccessDeniedPath = "/Identity/Account/AccessDenied";
  53. options.SlidingExpiration = true;
  54. });
  55. /**
  56. * =======================================================================================================================================================
  57. */
  58. builder.Services.AddScoped<ConfigRepository>();
  59. /**
  60. * =======================================================================================================================================================
  61. */
  62. // Add services to the container.
  63. builder.Services.AddControllersWithViews();
  64. builder.Logging.ClearProviders();
  65. builder.Logging.AddConsole();
  66. builder.Logging.AddDebug();
  67. var app = builder.Build();
  68. /**
  69. * =======================================================================================================================================================
  70. */
  71. app.UseMiddleware<Common>();
  72. // Configure the HTTP request pipeline.
  73. if (!app.Environment.IsDevelopment())
  74. {
  75. app.UseExceptionHandler("/Home/Error");
  76. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  77. app.UseHsts();
  78. }
  79. /**
  80. * =======================================================================================================================================================
  81. */
  82. app.UseHttpsRedirection();
  83. // 정적 파일을 제공하는 미들웨어 추가
  84. app.UseStaticFiles();
  85. app.UseStaticFiles(new StaticFileOptions
  86. {
  87. FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "node_modules")),
  88. RequestPath = "/node_modules"
  89. });
  90. /**
  91. * =======================================================================================================================================================
  92. */
  93. // 환경변수 불러오기
  94. var env = Environment.GetEnvironmentVariable("environmentVariables");
  95. // 환경변수를 static 변수로 저장
  96. app.Use(async (context, next) =>
  97. {
  98. context.Items["env"] = env;
  99. await next();
  100. });
  101. /**
  102. * =======================================================================================================================================================
  103. */
  104. app.UseRouting();
  105. app.UseAuthentication();
  106. app.UseAuthorization();
  107. app.MapRazorPages();
  108. app.MapControllerRoute(
  109. name: "default",
  110. pattern: "{controller=Home}/{action=Index}/{id?}");
  111. app.Run();