Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# SpeakingInBitsWeb
# 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`.
3 changes: 3 additions & 0 deletions SpeakingInBitsWeb.slnx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<Solution>
<Folder Name="/Solution Items/">
<File Path="README.md" />
</Folder>
<Project Path="SpeakingInBitsWeb/SpeakingInBitsWeb.csproj" />
</Solution>
57 changes: 57 additions & 0 deletions SpeakingInBitsWeb/Models/SeedData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.AspNetCore.Identity;

namespace SpeakingInBitsWeb.Models
{
/// <summary>
/// Provides methods for seeding initial data into the database.
/// </summary>
public static class SeedData
{
/// <summary>
/// Creates all necessary roles in the system if they don't already exist.
/// </summary>
/// <param name="roleManager">The role manager service.</param>
public static async Task CreateRolesAsync(RoleManager<IdentityRole> roleManager)
{
string[] roles = ["Instructor", "Student"];

foreach (var role in roles)
{
if (!await roleManager.RoleExistsAsync(role))
{
await roleManager.CreateAsync(new IdentityRole(role));
}
}
}

/// <summary>
/// Creates a default instructor user if one doesn't already exist.
/// </summary>
/// <param name="userManager">The user manager service.</param>
public static async Task CreateDefaultUserAsync(UserManager<ApplicationUser> 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");
}
}
}
}
}
12 changes: 12 additions & 0 deletions SpeakingInBitsWeb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>() // Add role support to Identity
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();

Expand Down Expand Up @@ -44,4 +45,15 @@
app.MapRazorPages()
.WithStaticAssets();

#if DEBUG
using (var scope = app.Services.CreateScope())
{
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();

await SeedData.CreateRolesAsync(roleManager);
await SeedData.CreateDefaultUserAsync(userManager);
}
#endif

app.Run();