Rename Project and only use Username as Authentication
This commit is contained in:
139
CouchLog/Program.cs
Normal file
139
CouchLog/Program.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using CouchLog.Components;
|
||||
using CouchLog.Components.Account;
|
||||
using CouchLog.Data;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
builder.Services.AddScoped<IdentityUserAccessor>();
|
||||
builder.Services.AddScoped<IdentityRedirectManager>();
|
||||
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
|
||||
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultScheme = IdentityConstants.ApplicationScheme;
|
||||
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
|
||||
})
|
||||
.AddIdentityCookies();
|
||||
|
||||
var connectionString = builder.Configuration.GetConnectionString("CouchLogDB") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
|
||||
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
||||
options.UseSqlite(connectionString));
|
||||
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
|
||||
|
||||
builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
|
||||
.AddRoles<IdentityRole>() // <-- Das ist der wichtige Zusatz
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||
.AddSignInManager()
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseMigrationsEndPoint();
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
|
||||
app.UseAntiforgery();
|
||||
|
||||
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
|
||||
|
||||
string[] roleNames = { "Admin", "User" };
|
||||
IdentityResult roleResult;
|
||||
|
||||
foreach (var roleName in roleNames)
|
||||
{
|
||||
var roleExist = await roleManager.RoleExistsAsync(roleName);
|
||||
if (!roleExist)
|
||||
{
|
||||
roleResult = await roleManager.CreateAsync(new IdentityRole(roleName));
|
||||
}
|
||||
}
|
||||
|
||||
// --- 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";
|
||||
|
||||
// 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
|
||||
};
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
else if (normalUser == null)
|
||||
{
|
||||
normalUser = new ApplicationUser
|
||||
{
|
||||
UserName = normalUsername,
|
||||
//Email = normalUserEmail,
|
||||
EmailConfirmed = true
|
||||
};
|
||||
var createResult = await userManager.CreateAsync(normalUser, adminPassword);
|
||||
|
||||
if (createResult.Succeeded)
|
||||
{
|
||||
await userManager.AddToRoleAsync(adminUser, "User");
|
||||
}
|
||||
}
|
||||
else if (!await userManager.IsInRoleAsync(normalUser, "User"))
|
||||
{
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user