fix user
This commit is contained in:
@@ -145,30 +145,31 @@ using (var scope = app.Services.CreateScope())
|
|||||||
// Dieser Block erstellt Rollen und initiale Benutzer, falls sie noch nicht existieren.
|
// 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!
|
// BITTE ENTFERNEN ODER KOMMENTIEREN SIE DIESEN BLOCK AUS, NACHDEM SIE IHRE ERSTEN BENUTZER ERSTELLT HABEN!
|
||||||
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
|
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
|
||||||
var userManager = services.GetRequiredService<UserManager<IdentityUser>>();
|
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
|
||||||
|
|
||||||
string[] roleNames = { "Admin", "Customer" };
|
string[] roleNames = { "Admin", "Customer" };
|
||||||
|
|
||||||
foreach (var roleName in roleNames)
|
foreach (var roleName in roleNames)
|
||||||
{
|
{
|
||||||
var roleExist = await roleManager.RoleExistsAsync(roleName);
|
if (!await roleManager.RoleExistsAsync(roleName))
|
||||||
if (!roleExist)
|
|
||||||
{
|
{
|
||||||
await roleManager.CreateAsync(new IdentityRole(roleName));
|
await roleManager.CreateAsync(new IdentityRole(roleName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Erstelle einen initialen Admin-Benutzer
|
// 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)
|
if (adminUser == null)
|
||||||
{
|
{
|
||||||
adminUser = new IdentityUser
|
// Erstellen Sie hier eine Instanz von ApplicationUser
|
||||||
|
adminUser = new ApplicationUser
|
||||||
{
|
{
|
||||||
UserName = "admin@yourwebshop.com", // << ANPASSEN >>
|
UserName = "admin@yourwebshop.com",
|
||||||
Email = "admin@yourwebshop.com", // << ANPASSEN >>
|
Email = "admin@yourwebshop.com",
|
||||||
EmailConfirmed = true
|
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)
|
if (createAdmin.Succeeded)
|
||||||
{
|
{
|
||||||
await userManager.AddToRoleAsync(adminUser, "Admin");
|
await userManager.AddToRoleAsync(adminUser, "Admin");
|
||||||
@@ -181,16 +182,18 @@ using (var scope = app.Services.CreateScope())
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Erstelle einen initialen Kunden-Benutzer
|
// 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)
|
if (customerUser == null)
|
||||||
{
|
{
|
||||||
customerUser = new IdentityUser
|
// Erstellen Sie auch hier eine Instanz von ApplicationUser
|
||||||
|
customerUser = new ApplicationUser
|
||||||
{
|
{
|
||||||
UserName = "customer@yourwebshop.com", // << ANPASSEN >>
|
UserName = "customer@yourwebshop.com",
|
||||||
Email = "customer@yourwebshop.com", // << ANPASSEN >>
|
Email = "customer@yourwebshop.com",
|
||||||
EmailConfirmed = true
|
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)
|
if (createCustomer.Succeeded)
|
||||||
{
|
{
|
||||||
await userManager.AddToRoleAsync(customerUser, "Customer");
|
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))}");
|
Console.WriteLine($"Error creating customer user: {string.Join(", ", createCustomer.Errors.Select(e => e.Description))}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// --- ENDE DES TEMPOR<4F>REN SETUP-BLOCKS ---
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ using Webshop.Domain.Entities;
|
|||||||
|
|
||||||
namespace Webshop.Infrastructure.Data
|
namespace Webshop.Infrastructure.Data
|
||||||
{
|
{
|
||||||
public class ApplicationDbContext : IdentityDbContext<IdentityUser>
|
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
|
||||||
{
|
{
|
||||||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
|
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user