From 50ef499073e8d568c857ba2b26176cc54f4a8eb1 Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Fri, 25 Jul 2025 11:45:16 +0200 Subject: [PATCH] fix user --- Webshop.Api/Program.cs | 34 ++++++++++--------- .../Data/ApplicationDbContext.cs | 2 +- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Webshop.Api/Program.cs b/Webshop.Api/Program.cs index 9339b08..aa1bb88 100644 --- a/Webshop.Api/Program.cs +++ b/Webshop.Api/Program.cs @@ -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>(); - var userManager = services.GetRequiredService>(); + var userManager = services.GetRequiredService>(); 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ÄREN SETUP-BLOCKS --- } catch (Exception ex) { diff --git a/Webshop.Infrastructure/Data/ApplicationDbContext.cs b/Webshop.Infrastructure/Data/ApplicationDbContext.cs index e09275b..3aa2801 100644 --- a/Webshop.Infrastructure/Data/ApplicationDbContext.cs +++ b/Webshop.Infrastructure/Data/ApplicationDbContext.cs @@ -5,7 +5,7 @@ using Webshop.Domain.Entities; namespace Webshop.Infrastructure.Data { - public class ApplicationDbContext : IdentityDbContext + public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) {