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. })
  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. */
  59. builder.Services.AddScoped<ConfigRepository>();
  60. /**
  61. * =======================================================================================================================================================
  62. */
  63. // Add services to the container.
  64. builder.Services.AddControllersWithViews();
  65. builder.Services.AddRazorPages();
  66. builder.Logging.ClearProviders();
  67. builder.Logging.AddConsole();
  68. builder.Logging.AddDebug();
  69. var app = builder.Build();
  70. /**
  71. * =======================================================================================================================================================
  72. */
  73. app.UseMiddleware<Common>();
  74. // Configure the HTTP request pipeline.
  75. if (!app.Environment.IsDevelopment())
  76. {
  77. app.UseExceptionHandler("/Home/Error");
  78. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  79. app.UseHsts();
  80. }
  81. /**
  82. * =======================================================================================================================================================
  83. */
  84. app.UseHttpsRedirection();
  85. // 정적 파일을 제공하는 미들웨어 추가
  86. app.UseStaticFiles();
  87. app.UseStaticFiles(new StaticFileOptions
  88. {
  89. FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "node_modules")),
  90. RequestPath = "/node_modules"
  91. });
  92. /**
  93. * =======================================================================================================================================================
  94. */
  95. // 환경변수 불러오기
  96. var env = Environment.GetEnvironmentVariable("environmentVariables");
  97. // 환경변수를 static 변수로 저장
  98. app.Use(async (context, next) =>
  99. {
  100. context.Items["env"] = env;
  101. await next();
  102. });
  103. /**
  104. * =======================================================================================================================================================
  105. */
  106. app.UseRouting();
  107. app.UseAuthentication();
  108. app.UseAuthorization();
  109. app.MapRazorPages();
  110. app.MapControllerRoute(
  111. name: "default",
  112. pattern: "{controller=Home}/{action=Index}/{id?}");
  113. app.Run();