From a03ff5c927e5c4adaad47719b35263f1b94e447f Mon Sep 17 00:00:00 2001 From: Joseph Ortiz Date: Fri, 3 Oct 2025 18:18:18 -0700 Subject: [PATCH 1/2] Add default roles and a default user with the instructor role on startup Enhanced ASP.NET Identity configuration to include role support by adding `.AddRoles()` in `Program.cs`. Introduced a `SeedData` class to seed predefined roles ("Instructor" and "Student") and a default instructor user during application startup in the development environment. --- SpeakingInBitsWeb/Models/SeedData.cs | 57 ++++++++++++++++++++++++++++ SpeakingInBitsWeb/Program.cs | 12 ++++++ 2 files changed, 69 insertions(+) create mode 100644 SpeakingInBitsWeb/Models/SeedData.cs diff --git a/SpeakingInBitsWeb/Models/SeedData.cs b/SpeakingInBitsWeb/Models/SeedData.cs new file mode 100644 index 0000000..61db0f2 --- /dev/null +++ b/SpeakingInBitsWeb/Models/SeedData.cs @@ -0,0 +1,57 @@ +using Microsoft.AspNetCore.Identity; + +namespace SpeakingInBitsWeb.Models +{ + /// + /// Provides methods for seeding initial data into the database. + /// + public static class SeedData + { + /// + /// Creates all necessary roles in the system if they don't already exist. + /// + /// The role manager service. + public static async Task CreateRolesAsync(RoleManager roleManager) + { + string[] roles = ["Instructor", "Student"]; + + foreach (var role in roles) + { + if (!await roleManager.RoleExistsAsync(role)) + { + await roleManager.CreateAsync(new IdentityRole(role)); + } + } + } + + /// + /// Creates a default instructor user if one doesn't already exist. + /// + /// The user manager service. + public static async Task CreateDefaultUserAsync(UserManager userManager) + { + string defaultEmail = "instructor@example.com"; + ApplicationUser? defaultUser = await userManager.FindByEmailAsync(defaultEmail); + + if (defaultUser == null) + { + ApplicationUser newUser = new() + { + UserName = "DefaultInstructor", + Email = defaultEmail, + EmailConfirmed = true, + FirstName = "Default", + LastName = "Instructor" + }; + + // Ensure password meets password strength requirements + IdentityResult result = await userManager.CreateAsync(newUser, "Programming01#"); + + if (result.Succeeded) + { + await userManager.AddToRoleAsync(newUser, "Instructor"); + } + } + } + } +} diff --git a/SpeakingInBitsWeb/Program.cs b/SpeakingInBitsWeb/Program.cs index fad9f1e..76d988c 100644 --- a/SpeakingInBitsWeb/Program.cs +++ b/SpeakingInBitsWeb/Program.cs @@ -12,6 +12,7 @@ builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) + .AddRoles() // Add role support to Identity .AddEntityFrameworkStores(); builder.Services.AddControllersWithViews(); @@ -44,4 +45,15 @@ app.MapRazorPages() .WithStaticAssets(); +#if DEBUG +using (var scope = app.Services.CreateScope()) +{ + var roleManager = scope.ServiceProvider.GetRequiredService>(); + var userManager = scope.ServiceProvider.GetRequiredService>(); + + await SeedData.CreateRolesAsync(roleManager); + await SeedData.CreateDefaultUserAsync(userManager); +} +#endif + app.Run(); From 2634184e6ae26ae55a1d21afc2d4249d28d00f90 Mon Sep 17 00:00:00 2001 From: Joseph Ortiz Date: Fri, 3 Oct 2025 18:24:54 -0700 Subject: [PATCH 2/2] Add instructor login info to README Added a section to the `README.md` detailing how to log in as an instructor in the development environment, including default credentials and a note about the seeded account. Updated the `SpeakingInBitsWeb.slnx` file to include the `README.md` under a new `/Solution Items/` folder. --- README.md | 11 ++++++++++- SpeakingInBitsWeb.slnx | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 74fc8fa..dda8578 100644 --- a/README.md +++ b/README.md @@ -1 +1,10 @@ -# SpeakingInBitsWeb \ No newline at end of file +# SpeakingInBitsWeb + +## Logging as instructor in development +To log in as an instructor in development, you can use the +following credentials: +- Username: `DefaultInstructor` +- Password: `Programming01#` + +*Note*: This account is only available in the development environment +and seeded within `/Models/SeedData.cs`. \ No newline at end of file diff --git a/SpeakingInBitsWeb.slnx b/SpeakingInBitsWeb.slnx index 0e40db4..bfc03a1 100644 --- a/SpeakingInBitsWeb.slnx +++ b/SpeakingInBitsWeb.slnx @@ -1,3 +1,6 @@ + + +