ServiceExtensions.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Microsoft.AspNetCore.Authentication.Cookies;
  2. using Microsoft.AspNetCore.Authentication.JwtBearer;
  3. using Microsoft.IdentityModel.Tokens;
  4. using System.Text;
  5. using Microsoft.AspNetCore.Identity;
  6. using bitforum.Models;
  7. using bitforum.Models.User;
  8. namespace bitforum.Extensions
  9. {
  10. public static class ServiceExtensions
  11. {
  12. // CORS 구성
  13. public static void ConfigureCors(this IServiceCollection services, IConfiguration configuration)
  14. {
  15. services.AddCors(options =>
  16. {
  17. options.AddPolicy("AllowFrontend", builder =>
  18. {
  19. builder.WithOrigins("https://localhost:3000").AllowAnyHeader().AllowAnyMethod().AllowCredentials();
  20. });
  21. });
  22. }
  23. // 관리자단 비밀번호 정책 구성
  24. public static void ConfigureIdentity(this IServiceCollection services, IConfiguration configuration)
  25. {
  26. services.AddIdentity<ApplicationUser, IdentityRole>(options =>
  27. {
  28. options.SignIn.RequireConfirmedAccount = true; // 이메일 확인 비활성화
  29. // Password settings.
  30. options.Password.RequireDigit = true;
  31. options.Password.RequireLowercase = true;
  32. options.Password.RequireNonAlphanumeric = true;
  33. options.Password.RequireUppercase = true;
  34. options.Password.RequiredLength = 6;
  35. options.Password.RequiredUniqueChars = 1;
  36. // Lockout settings.
  37. options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
  38. options.Lockout.MaxFailedAccessAttempts = 5;
  39. options.Lockout.AllowedForNewUsers = true;
  40. // User settings.
  41. options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
  42. options.User.RequireUniqueEmail = false;
  43. })
  44. .AddEntityFrameworkStores<UserContext>() // 사용자 계정 저장소
  45. .AddDefaultUI() // 기본 UI 사용
  46. .AddDefaultTokenProviders(); // 기본 토큰 제공자 사용
  47. 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. // JWT 전달자 인증 구성
  57. public static void ConfigureAuthentication(this IServiceCollection services, IConfiguration configuration)
  58. {
  59. services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
  60. {
  61. options.TokenValidationParameters = new TokenValidationParameters
  62. {
  63. ValidateIssuerSigningKey = true,
  64. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Setting.Jwt.SecretKey)),
  65. ValidateIssuer = true,
  66. ValidateAudience = true,
  67. ValidIssuer = Setting.Jwt.Issuer,
  68. ValidAudience = Setting.Jwt.Audience,
  69. ValidateLifetime = true,
  70. ClockSkew = TimeSpan.Zero
  71. };
  72. options.Events = new JwtBearerEvents
  73. {
  74. OnMessageReceived = context =>
  75. {
  76. if (context.Request.Cookies.ContainsKey("accessToken"))
  77. {
  78. context.Token = context.Request.Cookies["accessToken"];
  79. }
  80. return Task.CompletedTask;
  81. }
  82. };
  83. });
  84. }
  85. }
  86. }