From 535158e68323a757c5619097ae1db09992f334ee Mon Sep 17 00:00:00 2001 From: Joseph Ortiz Date: Fri, 3 Oct 2025 19:04:25 -0700 Subject: [PATCH] Refactor Identity configuration Centralized ASP.NET Core Identity configuration in a new `IdentityConfiguration.cs` class. Introduced a static method `ConfigureIdentityOptions` to manage password, lockout, user, and sign-in settings, improving maintainability and reusability. Updated `Program.cs` to use the centralized configuration. Relaxed password and sign-in requirements for development purposes. Ensured `ApplicationDbContext` is used as the Identity store. --- .../Models/IdentityConfiguration.cs | 40 +++++++++++++++++++ SpeakingInBitsWeb/Program.cs | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 SpeakingInBitsWeb/Models/IdentityConfiguration.cs diff --git a/SpeakingInBitsWeb/Models/IdentityConfiguration.cs b/SpeakingInBitsWeb/Models/IdentityConfiguration.cs new file mode 100644 index 0000000..9814ccd --- /dev/null +++ b/SpeakingInBitsWeb/Models/IdentityConfiguration.cs @@ -0,0 +1,40 @@ +using Microsoft.AspNetCore.Identity; + +namespace SpeakingInBitsWeb.Models +{ + /// + /// Provides configuration methods for ASP.NET Core Identity options. + /// + public static class IdentityConfiguration + { + /// + /// Configures the Identity options for the application. + /// + /// The IdentityOptions to configure. + public static void ConfigureIdentityOptions(IdentityOptions options) + { + // Password settings + options.Password.RequireDigit = false; + options.Password.RequireLowercase = false; + options.Password.RequireNonAlphanumeric = false; + options.Password.RequireUppercase = false; + options.Password.RequiredLength = 8; + options.Password.RequiredUniqueChars = 3; + + // 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; + + // Sign in settings + options.SignIn.RequireConfirmedAccount = false; + options.SignIn.RequireConfirmedEmail = false; + options.SignIn.RequireConfirmedPhoneNumber = false; + } + } +} diff --git a/SpeakingInBitsWeb/Program.cs b/SpeakingInBitsWeb/Program.cs index 76d988c..5093feb 100644 --- a/SpeakingInBitsWeb/Program.cs +++ b/SpeakingInBitsWeb/Program.cs @@ -11,7 +11,7 @@ options.UseSqlServer(connectionString)); builder.Services.AddDatabaseDeveloperPageExceptionFilter(); -builder.Services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) +builder.Services.AddDefaultIdentity(IdentityConfiguration.ConfigureIdentityOptions) .AddRoles() // Add role support to Identity .AddEntityFrameworkStores(); builder.Services.AddControllersWithViews();