using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using MTG_CollectionVerwaltung.API_Connector; using MTG_CollectionVerwaltung.Data; using MTG_CollectionVerwaltung.Data.MTGCollection; SQLitePCL.Batteries_V2.Init(); var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddDbContext(options => options.UseSqlite("Data Source=mtgcollection.db")); /*var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); builder.Services.AddDbContext(options => options.UseSqlServer(connectionString));*/ builder.Services.AddDbContext(options => options.UseSqlite("Data Source=mtgcollection.db")); builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) .AddRoles() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); builder.Services.AddRazorPages(); builder.Services.AddHostedService(); var app = builder.Build(); //Generate and actualize DBs at Powerup using (var scope = app.Services.CreateScope()) { var mtgContext = scope.ServiceProvider.GetRequiredService(); mtgContext.Database.Migrate(); // erstellt DB + Tabellen falls nötig var identityContext = scope.ServiceProvider.GetRequiredService(); identityContext.Database.Migrate(); } //Admin User ertsellen wenn nicht vorhanden using (var scope = app.Services.CreateScope()) { var services = scope.ServiceProvider; var userManager = services.GetRequiredService>(); var roleManager = services.GetRequiredService>(); var adminEmail = builder.Configuration["AdminUser:Email"]; var adminPassword = "ChangeMe!123"; // Rolle "Admin" anlegen, falls nicht vorhanden if (!await roleManager.RoleExistsAsync("Admin")) { await roleManager.CreateAsync(new IdentityRole("Admin")); } var adminUser = await userManager.FindByEmailAsync(adminEmail); if (adminUser == null) { adminUser = new ApplicationUser { UserName = adminEmail, Email = adminEmail, EmailConfirmed = true, MustChangePassword = true }; var result = await userManager.CreateAsync(adminUser, adminPassword); if (result.Succeeded) { await userManager.AddToRoleAsync(adminUser, "Admin"); } } else { if (!await userManager.IsInRoleAsync(adminUser, "Admin")) { await userManager.AddToRoleAsync(adminUser, "Admin"); } } } // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseMigrationsEndPoint(); } else { app.UseExceptionHandler("/Error"); // 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.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapRazorPages(); app.Run();