| | | 1 | | using System.IO; |
| | | 2 | | using Microsoft.AspNetCore.DataProtection; |
| | | 3 | | using Microsoft.AspNetCore.Hosting; |
| | | 4 | | using Microsoft.AspNetCore.Identity; |
| | | 5 | | using Microsoft.Extensions.Hosting; |
| | | 6 | | using OzonRepositories.Context.Identity; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.Extensions; |
| | | 9 | | |
| | | 10 | | public static class IdentityExtensions |
| | | 11 | | { |
| | | 12 | | public static void AddIdentityServices(this IServiceCollection services, IWebHostEnvironment environment, IConfigura |
| | | 13 | | { |
| | | 14 | | // DataProtection keys must persist across restarts/deploys, |
| | | 15 | | // otherwise auth/session cookies cannot be decrypted ("key was not found in the key ring"). |
| | 4 | 16 | | IDataProtectionBuilder dataProtectionBuilder = services |
| | 4 | 17 | | .AddDataProtection() |
| | 4 | 18 | | .SetApplicationName("Studio2Prod"); |
| | | 19 | | |
| | 4 | 20 | | var keysPath = Environment.GetEnvironmentVariable("ASPNETCORE_DATA_PROTECTION_KEYS_PATH"); |
| | 4 | 21 | | if (string.IsNullOrWhiteSpace(keysPath)) |
| | | 22 | | { |
| | 4 | 23 | | keysPath = Path.Combine(environment.ContentRootPath, "DataProtection-Keys"); |
| | | 24 | | } |
| | | 25 | | |
| | 4 | 26 | | var directory = new DirectoryInfo(keysPath); |
| | 4 | 27 | | if (!directory.Exists) |
| | | 28 | | { |
| | 4 | 29 | | directory.Create(); |
| | | 30 | | } |
| | | 31 | | |
| | 4 | 32 | | dataProtectionBuilder.PersistKeysToFileSystem(directory); |
| | | 33 | | |
| | 4 | 34 | | if (environment.IsDevelopment()) |
| | | 35 | | { |
| | 4 | 36 | | Console.WriteLine($"[DataProtection] Keys directory: {directory.FullName}"); |
| | | 37 | | } |
| | | 38 | | |
| | 4 | 39 | | services.AddDefaultIdentity<CustomIdentityUser>() |
| | 4 | 40 | | .AddDefaultTokenProviders() |
| | 4 | 41 | | .AddRoles<IdentityRole>() |
| | 4 | 42 | | .AddEntityFrameworkStores<OzonIdentityOrderContext>(); |
| | | 43 | | |
| | | 44 | | // Настройка блокировки аккаунта после неудачных попыток входа |
| | 4 | 45 | | services.Configure<IdentityOptions>(options => |
| | 4 | 46 | | { |
| | 4 | 47 | | // Блокировка аккаунта: максимум 5 попыток, блокировка на 15 минут |
| | 1 | 48 | | options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15); |
| | 1 | 49 | | options.Lockout.MaxFailedAccessAttempts = 5; |
| | 1 | 50 | | options.Lockout.AllowedForNewUsers = true; |
| | 4 | 51 | | |
| | 4 | 52 | | // Для внутренних пользователей не требуем спецсимвол в пароле. |
| | 1 | 53 | | options.Password.RequireNonAlphanumeric = false; |
| | 1 | 54 | | options.SignIn.RequireConfirmedEmail = true; |
| | 5 | 55 | | }); |
| | | 56 | | |
| | | 57 | | // Базовые настройки Cookie (детальные настройки безопасности в SecurityExtensions) |
| | 4 | 58 | | services.ConfigureApplicationCookie(options => |
| | 4 | 59 | | { |
| | 0 | 60 | | options.Cookie.Name = ".AspNetCore.Identity.Application"; |
| | 0 | 61 | | options.LoginPath = "/Identity/Account/Login"; |
| | 0 | 62 | | options.LogoutPath = "/Identity/Account/Logout"; |
| | 0 | 63 | | options.AccessDeniedPath = "/Identity/Account/AccessDenied"; |
| | 4 | 64 | | // Детальные настройки безопасности (HttpOnly, Secure, SameSite) настраиваются в SecurityExtensions |
| | 4 | 65 | | }); |
| | | 66 | | |
| | 4 | 67 | | var googleClientId = configuration["Authentication:Google:ClientId"]; |
| | 4 | 68 | | var googleClientSecret = configuration["Authentication:Google:ClientSecret"]; |
| | 4 | 69 | | if (!string.IsNullOrWhiteSpace(googleClientId) && !string.IsNullOrWhiteSpace(googleClientSecret)) |
| | | 70 | | { |
| | 1 | 71 | | services |
| | 1 | 72 | | .AddAuthentication() |
| | 1 | 73 | | .AddGoogle(options => |
| | 1 | 74 | | { |
| | 0 | 75 | | options.ClientId = googleClientId; |
| | 0 | 76 | | options.ClientSecret = googleClientSecret; |
| | 1 | 77 | | }); |
| | | 78 | | } |
| | 4 | 79 | | } |
| | | 80 | | } |