| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using Microsoft.Extensions.FileProviders; // 정적파일 관련
- using Microsoft.AspNetCore.Identity;
- using Microsoft.AspNetCore.Identity.UI.Services;
- using Microsoft.EntityFrameworkCore;
- using bitforum.Middleware;
- using bitforum.Services;
- using bitforum.Repository;
- using bitforum.Models.User;
- var builder = WebApplication.CreateBuilder(args);
- builder.Services.AddTransient<IEmailSender, EmailSender>();
- // Add http context accessor
- builder.Services.AddHttpContextAccessor();
- /**
- * =======================================================================================================================================================
- */
- // DbContext 설정
- var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
- builder.Services.AddDbContext<DefaultDbContext>(options => options.UseSqlServer(connectionString));
- builder.Services.AddDbContext<UserContext>(options => options.UseSqlServer(connectionString));
- /**
- * =======================================================================================================================================================
- */
- // Identity 서비스 추가
- builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
- {
- options.SignIn.RequireConfirmedAccount = true; // 이메일 확인 비활성화
- })
- .AddEntityFrameworkStores<UserContext>()
- .AddDefaultUI()
- .AddDefaultTokenProviders();
- builder.Services.Configure<IdentityOptions>(options =>
- {
- // Password settings.
- options.Password.RequireDigit = true;
- options.Password.RequireLowercase = true;
- options.Password.RequireNonAlphanumeric = true;
- options.Password.RequireUppercase = true;
- options.Password.RequiredLength = 6;
- options.Password.RequiredUniqueChars = 1;
- // Lockout settings.
- options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
- options.Lockout.MaxFailedAccessAttempts = 5;
- options.Lockout.AllowedForNewUsers = true;
- // User settings.
- options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
- options.User.RequireUniqueEmail = false;
- });
- builder.Services.ConfigureApplicationCookie(options =>
- {
- // Cookie settings
- options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
- options.LoginPath = "/Identity/Account/Login";
- options.AccessDeniedPath = "/Identity/Account/AccessDenied";
- options.SlidingExpiration = true;
- });
- /**
- * =======================================================================================================================================================
- */
- builder.Services.AddScoped<ConfigRepository>();
- /**
- * =======================================================================================================================================================
- */
- // Add services to the container.
- builder.Services.AddControllersWithViews();
- builder.Services.AddRazorPages();
- builder.Logging.ClearProviders();
- builder.Logging.AddConsole();
- builder.Logging.AddDebug();
- var app = builder.Build();
- /**
- * =======================================================================================================================================================
- */
- app.UseMiddleware<Common>();
- // Configure the HTTP request pipeline.
- if (!app.Environment.IsDevelopment())
- {
- app.UseExceptionHandler("/Home/Error");
- // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
- app.UseHsts();
- }
- /**
- * =======================================================================================================================================================
- */
- app.UseHttpsRedirection();
- // 정적 파일을 제공하는 미들웨어 추가
- app.UseStaticFiles();
- app.UseStaticFiles(new StaticFileOptions
- {
- FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "node_modules")),
- RequestPath = "/node_modules"
- });
- /**
- * =======================================================================================================================================================
- */
- // 환경변수 불러오기
- var env = Environment.GetEnvironmentVariable("environmentVariables");
- // 환경변수를 static 변수로 저장
- app.Use(async (context, next) =>
- {
- context.Items["env"] = env;
- await next();
- });
- /**
- * =======================================================================================================================================================
- */
- app.UseRouting();
- app.UseAuthentication();
- app.UseAuthorization();
- app.MapRazorPages();
- app.MapControllerRoute(
- name: "default",
- pattern: "{controller=Home}/{action=Index}/{id?}");
- app.Run();
|