This commit is contained in:
Tizian.Breuch
2025-07-25 11:45:16 +02:00
parent a9adaff3eb
commit 50ef499073
2 changed files with 19 additions and 17 deletions

View File

@@ -145,30 +145,31 @@ using (var scope = app.Services.CreateScope())
// Dieser Block erstellt Rollen und initiale Benutzer, falls sie noch nicht existieren.
// BITTE ENTFERNEN ODER KOMMENTIEREN SIE DIESEN BLOCK AUS, NACHDEM SIE IHRE ERSTEN BENUTZER ERSTELLT HABEN!
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
var userManager = services.GetRequiredService<UserManager<IdentityUser>>();
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
string[] roleNames = { "Admin", "Customer" };
foreach (var roleName in roleNames)
{
var roleExist = await roleManager.RoleExistsAsync(roleName);
if (!roleExist)
if (!await roleManager.RoleExistsAsync(roleName))
{
await roleManager.CreateAsync(new IdentityRole(roleName));
}
}
// Erstelle einen initialen Admin-Benutzer
var adminUser = await userManager.FindByEmailAsync("admin@yourwebshop.com"); // << ANPASSEN >>
var adminUser = await userManager.FindByEmailAsync("admin@yourwebshop.com");
if (adminUser == null)
{
adminUser = new IdentityUser
// Erstellen Sie hier eine Instanz von ApplicationUser
adminUser = new ApplicationUser
{
UserName = "admin@yourwebshop.com", // << ANPASSEN >>
Email = "admin@yourwebshop.com", // << ANPASSEN >>
EmailConfirmed = true
UserName = "admin@yourwebshop.com",
Email = "admin@yourwebshop.com",
EmailConfirmed = true,
CreatedDate = DateTimeOffset.UtcNow // Setzen Sie Ihr neues Feld!
};
var createAdmin = await userManager.CreateAsync(adminUser, "SecureAdminPass123!"); // << ANPASSEN >>
var createAdmin = await userManager.CreateAsync(adminUser, "SecureAdminPass123!");
if (createAdmin.Succeeded)
{
await userManager.AddToRoleAsync(adminUser, "Admin");
@@ -181,16 +182,18 @@ using (var scope = app.Services.CreateScope())
}
// Erstelle einen initialen Kunden-Benutzer
var customerUser = await userManager.FindByEmailAsync("customer@yourwebshop.com"); // << ANPASSEN >>
var customerUser = await userManager.FindByEmailAsync("customer@yourwebshop.com");
if (customerUser == null)
{
customerUser = new IdentityUser
// Erstellen Sie auch hier eine Instanz von ApplicationUser
customerUser = new ApplicationUser
{
UserName = "customer@yourwebshop.com", // << ANPASSEN >>
Email = "customer@yourwebshop.com", // << ANPASSEN >>
EmailConfirmed = true
UserName = "customer@yourwebshop.com",
Email = "customer@yourwebshop.com",
EmailConfirmed = true,
CreatedDate = DateTimeOffset.UtcNow // Setzen Sie Ihr neues Feld!
};
var createCustomer = await userManager.CreateAsync(customerUser, "SecureCustomerPass123!"); // << ANPASSEN >>
var createCustomer = await userManager.CreateAsync(customerUser, "SecureCustomerPass123!");
if (createCustomer.Succeeded)
{
await userManager.AddToRoleAsync(customerUser, "Customer");
@@ -201,7 +204,6 @@ using (var scope = app.Services.CreateScope())
Console.WriteLine($"Error creating customer user: {string.Join(", ", createCustomer.Errors.Select(e => e.Description))}");
}
}
// --- ENDE DES TEMPOR<4F>REN SETUP-BLOCKS ---
}
catch (Exception ex)
{