Adds a solid start Foundation with a GlobalList.razor and OnStartUp.cs
This commit is contained in:
@@ -7,7 +7,6 @@ using CouchLog.Data;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
|
||||
@@ -29,7 +28,7 @@ builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
||||
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
|
||||
|
||||
builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
|
||||
.AddRoles<IdentityRole>() // <-- Das ist der wichtige Zusatz
|
||||
.AddRoles<IdentityRole>()
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||
.AddSignInManager()
|
||||
.AddDefaultTokenProviders();
|
||||
@@ -38,7 +37,6 @@ builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSe
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseMigrationsEndPoint();
|
||||
@@ -46,7 +44,6 @@ if (app.Environment.IsDevelopment())
|
||||
else
|
||||
{
|
||||
app.UseExceptionHandler("/Error", createScopeForErrors: true);
|
||||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
@@ -59,13 +56,12 @@ app.MapStaticAssets();
|
||||
app.MapRazorComponents<App>()
|
||||
.AddInteractiveServerRenderMode();
|
||||
|
||||
// Add additional endpoints required by the Identity /Account Razor components.
|
||||
app.MapAdditionalIdentityEndpoints();
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
|
||||
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>(); // UserManager hinzufügen
|
||||
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
|
||||
string[] roleNames = { "Admin", "User" };
|
||||
IdentityResult roleResult;
|
||||
@@ -79,32 +75,22 @@ using (var scope = app.Services.CreateScope())
|
||||
}
|
||||
}
|
||||
|
||||
// --- HIER BEGINNT DER NEUE TEIL ---
|
||||
// Erstellt den Admin-Benutzer und weist ihm die Admin-Rolle zu.
|
||||
// WICHTIG: Ändere hier die E-Mail-Adresse und das Passwort!
|
||||
var adminUsername = "Admin";
|
||||
var normalUsername = "User";
|
||||
var adminEmail = "admin@deine-app.de";
|
||||
var adminPassword = "EinSehrSicheresPasswort123!"; // Nur für lokale Entwicklung, besser aus Konfiguration laden
|
||||
var normalUserEmail = "user@deine-app.de";
|
||||
var adminPassword = "EinSehrSicheresPasswort123!";
|
||||
|
||||
// Sucht nach dem Benutzer anhand der E-Mail.
|
||||
var adminUser = await userManager.FindByNameAsync(adminUsername);
|
||||
var normalUser = await userManager.FindByNameAsync(normalUsername);
|
||||
|
||||
// Wenn der Admin-Benutzer NICHT existiert, erstellen wir ihn.
|
||||
if (adminUser == null)
|
||||
{
|
||||
adminUser = new ApplicationUser
|
||||
{
|
||||
UserName = adminUsername,
|
||||
//Email = adminEmail,
|
||||
EmailConfirmed = true // Wichtig, damit er sich direkt einloggen kann
|
||||
EmailConfirmed = true
|
||||
};
|
||||
// Erstellt den Benutzer mit dem definierten Passwort.
|
||||
var createResult = await userManager.CreateAsync(adminUser, adminPassword);
|
||||
|
||||
// Wenn die Erstellung erfolgreich war, weisen wir die Admin-Rolle zu.
|
||||
|
||||
if (createResult.Succeeded)
|
||||
{
|
||||
await userManager.AddToRoleAsync(adminUser, "Admin");
|
||||
@@ -115,7 +101,6 @@ using (var scope = app.Services.CreateScope())
|
||||
normalUser = new ApplicationUser
|
||||
{
|
||||
UserName = normalUsername,
|
||||
//Email = normalUserEmail,
|
||||
EmailConfirmed = true
|
||||
};
|
||||
var createResult = await userManager.CreateAsync(normalUser, adminPassword);
|
||||
@@ -129,11 +114,39 @@ using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
await userManager.AddToRoleAsync(normalUser, "User");
|
||||
}
|
||||
// Optional: Wenn der Benutzer bereits existiert, aber kein Admin ist.
|
||||
else if (!await userManager.IsInRoleAsync(adminUser, "Admin"))
|
||||
{
|
||||
await userManager.AddToRoleAsync(adminUser, "Admin");
|
||||
}
|
||||
|
||||
var CouchLogDB = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
|
||||
MediaType mediaType = new()
|
||||
{
|
||||
Name = "Movie"
|
||||
};
|
||||
|
||||
if (!CouchLogDB.MediaType.Any())
|
||||
CouchLogDB.MediaType.Add(mediaType);
|
||||
|
||||
await CouchLogDB.SaveChangesAsync();
|
||||
|
||||
GlobalEntity globalEntity = new()
|
||||
{
|
||||
Title = "Inception",
|
||||
CreationTime = DateTime.Now,
|
||||
CreatorId = (await userManager.FindByNameAsync("Admin"))!.Id,
|
||||
TypeId = 1,
|
||||
PicturePath = "Pictures/Inception.jpg"
|
||||
};
|
||||
|
||||
if (!CouchLogDB.GlobalEntities.Any())
|
||||
CouchLogDB.GlobalEntities.Add(globalEntity);
|
||||
|
||||
await CouchLogDB.SaveChangesAsync();
|
||||
|
||||
}
|
||||
|
||||
Console.WriteLine("ALL UP");
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user