300 lines
13 KiB
C#
300 lines
13 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.HttpOverrides; // For UseForwardedHeaders
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging; // For ILogger
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models; // For Swagger OpenAPI models
|
|
using Resend;
|
|
using System.Text;
|
|
using System.Text.Json.Serialization;
|
|
using Webshop.Api.SwaggerFilters; // For AuthorizeOperationFilter
|
|
using Webshop.Application.Services.Admin; // AdminUserService, AdminProductService
|
|
using Webshop.Application.Services.Admin.Interfaces;
|
|
using Webshop.Application.Services.Auth; // IAuthService, AuthService
|
|
using Webshop.Application.Services.Customers;
|
|
using Webshop.Application.Services.Customers.Interfaces;
|
|
using Webshop.Application.Services.Public; // ProductService
|
|
using Webshop.Application.Services.Public.Interfaces;
|
|
using Webshop.Domain.Entities;
|
|
using Webshop.Domain.Identity;
|
|
using Webshop.Domain.Interfaces; // IProductRepository
|
|
using Webshop.Infrastructure.Data; // ApplicationDbContext
|
|
using Webshop.Infrastructure.Repositories; // ProductRepository
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// --- START: DIENSTE ZUM CONTAINER HINZUFÜGEN ---
|
|
|
|
// 1. Datenbank-Kontext (DbContext) registrieren
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))
|
|
);
|
|
|
|
// 2. ASP.NET Core Identity für Benutzerverwaltung registrieren
|
|
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
|
|
{
|
|
options.SignIn.RequireConfirmedAccount = true;
|
|
})
|
|
.AddEntityFrameworkStores<ApplicationDbContext>() // Stellen Sie sicher, dass Ihr DbContext-Name hier korrekt ist
|
|
.AddDefaultTokenProviders();
|
|
|
|
// Optional: Passe die Anforderungen für Passwörter für die Entwicklung an
|
|
builder.Services.Configure<IdentityOptions>(options =>
|
|
{
|
|
options.Password.RequireDigit = false;
|
|
options.Password.RequiredLength = 6;
|
|
options.Password.RequireNonAlphanumeric = false;
|
|
options.Password.RequireUppercase = false;
|
|
options.Password.RequireLowercase = false;
|
|
options.SignIn.RequireConfirmedEmail = true; // Für einfache Entwicklung/Tests
|
|
});
|
|
|
|
|
|
// 3. JWT-Authentifizierung konfigurieren
|
|
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
|
|
var secretKey = jwtSettings["Secret"] ?? throw new InvalidOperationException("JWT Secret not found in configuration.");
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = jwtSettings["Issuer"],
|
|
ValidAudience = jwtSettings["Audience"],
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey))
|
|
};
|
|
});
|
|
builder.Services.AddAuthorization(); // Aktiviert die Autorisierung
|
|
|
|
// 4. Unsere eigenen Interfaces und Klassen registrieren (Dependency Injection)
|
|
// Repositories
|
|
builder.Services.AddScoped<IProductRepository, ProductRepository>();
|
|
builder.Services.AddScoped<ISupplierRepository, SupplierRepository>();
|
|
builder.Services.AddScoped<ICustomerRepository, CustomerRepository>();
|
|
builder.Services.AddScoped<IPaymentMethodRepository, PaymentMethodRepository>();
|
|
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
|
|
builder.Services.AddScoped<IOrderRepository, OrderRepository>();
|
|
builder.Services.AddScoped<IShippingMethodRepository, ShippingMethodRepository>();
|
|
|
|
// AUTH Services
|
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
|
|
|
// PUBLIC Services
|
|
builder.Services.AddScoped<IProductService, ProductService>();
|
|
builder.Services.AddScoped<IPaymentMethodService, PaymentMethodService>();
|
|
builder.Services.AddScoped<ICategoryService, CategoryService>();
|
|
builder.Services.AddScoped<ICategoryService, CategoryService>();
|
|
|
|
// ADMIN Services
|
|
builder.Services.AddScoped<IAdminUserService, AdminUserService>();
|
|
builder.Services.AddScoped<IAdminProductService, AdminProductService>();
|
|
builder.Services.AddScoped<IAdminSupplierService, AdminSupplierService>();
|
|
builder.Services.AddScoped<IAdminPaymentMethodService, AdminPaymentMethodService>();
|
|
builder.Services.AddScoped<IAdminCategoryService, AdminCategoryService>(); // Hinzugefügt für Konsistenz
|
|
builder.Services.AddScoped<IAdminOrderService, AdminOrderService>();
|
|
|
|
//builder.Services.AddScoped<IAdminDiscountService, AdminDiscountService>(); // Hinzugefügt für Konsistenz
|
|
builder.Services.AddScoped<IAdminOrderService, AdminOrderService>(); // Hinzugefügt für Konsistenz
|
|
//builder.Services.AddScoped<IAdminSettingService, AdminSettingService>(); // Hinzugefügt für Konsistenz
|
|
|
|
// CUSTOMER Services (später Implementierungen hinzufügen)
|
|
builder.Services.AddScoped<ICustomerService, CustomerService>();
|
|
builder.Services.AddScoped<IOrderService, OrderService>(); // Hinzugefügt für Konsistenz
|
|
//builder.Services.AddScoped<ICheckoutService, CheckoutService>(); // Hinzugefügt für Konsistenz
|
|
//builder.Services.AddScoped<IReviewService, ReviewService>(); // Hinzugefügt für Konsistenz
|
|
|
|
// --- NEU: Resend E-Mail-Dienst konfigurieren ---
|
|
builder.Services.AddOptions(); // Stellt sicher, dass Options konfiguriert werden können
|
|
builder.Services.AddHttpClient<ResendClient>(); // Fügt HttpClient für Resend hinzu
|
|
builder.Services.Configure<ResendClientOptions>(options =>
|
|
{
|
|
// Der API-Token kommt aus den Konfigurationen (z.B. appsettings.json oder Umgebungsvariablen)
|
|
options.ApiToken = builder.Configuration["Resend:ApiToken"]!; // << ANPASSEN >>
|
|
});
|
|
builder.Services.AddTransient<IResend, ResendClient>();
|
|
|
|
|
|
// 5. Controller und Swagger/OpenAPI hinzufügen
|
|
builder.Services.AddControllers()
|
|
.AddJsonOptions(options =>
|
|
{
|
|
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
|
});
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
// 1. JWT Security Definition hinzufügen
|
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
|
|
Name = "Authorization",
|
|
In = ParameterLocation.Header, // Der Token wird im Header gesendet
|
|
Type = SecuritySchemeType.Http, // Dies ist ein HTTP-Schema
|
|
Scheme = "Bearer" // Das Schema ist "Bearer"
|
|
});
|
|
|
|
// 2. Security Requirement für alle Operationen hinzufügen
|
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = "Bearer" // Verweist auf die oben definierte "Bearer" Sicherheit
|
|
}
|
|
},
|
|
new string[] {} // Keine spezifischen "Scopes" für JWT (leer lassen)
|
|
}
|
|
});
|
|
c.OperationFilter<AuthorizeOperationFilter>();
|
|
|
|
c.SchemaFilter<AddExampleSchemaFilter>();
|
|
|
|
c.OperationFilter<LoginExampleOperationFilter>();
|
|
c.OperationFilter<PaymentMethodExampleOperationFilter>();
|
|
c.OperationFilter<SupplierExampleOperationFilter>();
|
|
});
|
|
|
|
// --- ENDE: DIENSTE ZUM CONTAINER HINZUFÜGEN ---
|
|
|
|
var app = builder.Build(); // <-- Hier wird die App gebaut
|
|
|
|
// OPTIONALE BLÖCKE FÜR MIGRATION UND BENUTZERINITIALISIERUNG - DIESER CODE WIRD VOR APP.RUN() AUSGEFÜHRT
|
|
// OPTIONALE BLÖCKE FÜR MIGRATION UND BENUTZERINITIALISIERUNG - DIESER CODE WIRD VOR APP.RUN() AUSGEFÜHRT
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var services = scope.ServiceProvider;
|
|
try
|
|
{
|
|
var context = services.GetRequiredService<ApplicationDbContext>();
|
|
// Wendet ausstehende Datenbank-Migrationen an (sehr nützlich für Entwicklung/Tests)
|
|
context.Database.Migrate();
|
|
|
|
// --- TEMPORÄRER INITIALER ADMIN- UND KUNDEN-SETUP (NUR FÜR ERSTE ENTWICKLUNG!) ---
|
|
// 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<RoleManager<IdentityRole>>();
|
|
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>(); // << KORREKT: UserManager für ApplicationUser >>
|
|
|
|
string[] roleNames = { "Admin", "Customer" };
|
|
|
|
foreach (var roleName in roleNames)
|
|
{
|
|
if (!await roleManager.RoleExistsAsync(roleName))
|
|
{
|
|
await roleManager.CreateAsync(new IdentityRole(roleName));
|
|
}
|
|
}
|
|
|
|
// Erstelle einen initialen Admin-Benutzer und sein Customer-Profil
|
|
var adminUser = await userManager.FindByEmailAsync("admin@yourwebshop.com"); // << ANPASSEN >>
|
|
if (adminUser == null)
|
|
{
|
|
adminUser = new ApplicationUser // << KORREKT: ERSTELLT ApplicationUser >>
|
|
{
|
|
UserName = "admin@yourwebshop.com", // << ANPASSEN >>
|
|
Email = "admin@yourwebshop.com", // << ANPASSEN >>
|
|
EmailConfirmed = true,
|
|
CreatedDate = DateTimeOffset.UtcNow, // Custom Property auf ApplicationUser
|
|
LastActive = DateTimeOffset.UtcNow // Custom Property auf ApplicationUser
|
|
};
|
|
var createAdmin = await userManager.CreateAsync(adminUser, "SecureAdminPass123!"); // << ANPASSEN >>
|
|
if (createAdmin.Succeeded)
|
|
{
|
|
await userManager.AddToRoleAsync(adminUser, "Admin");
|
|
Console.WriteLine("Admin user created.");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"Error creating admin user: {string.Join(", ", createAdmin.Errors.Select(e => e.Description))}");
|
|
}
|
|
}
|
|
|
|
// Erstelle einen initialen Kunden-Benutzer und sein Customer-Profil (KOMBINIERT)
|
|
var customerUser = await userManager.FindByEmailAsync("customer@yourwebshop.com"); // << ANPASSEN >>
|
|
if (customerUser == null)
|
|
{
|
|
customerUser = new ApplicationUser // << KORREKT: ERSTELLT ApplicationUser >>
|
|
{
|
|
UserName = "customer@yourwebshop.com", // << ANPASSEN >>
|
|
Email = "customer@yourwebshop.com", // << ANPASSEN >>
|
|
EmailConfirmed = true,
|
|
CreatedDate = DateTimeOffset.UtcNow, // Custom Property auf ApplicationUser
|
|
LastActive = DateTimeOffset.UtcNow // Custom Property auf ApplicationUser
|
|
};
|
|
var createCustomer = await userManager.CreateAsync(customerUser, "SecureCustomerPass123!"); // << ANPASSEN >>
|
|
if (createCustomer.Succeeded)
|
|
{
|
|
await userManager.AddToRoleAsync(customerUser, "Customer");
|
|
Console.WriteLine("Customer user created.");
|
|
|
|
// Kombinierter Teil: Customer-Profil erstellen, direkt nach IdentityUser-Erstellung
|
|
var customerProfile = await context.Customers.FirstOrDefaultAsync(c => c.AspNetUserId == customerUser.Id); // << KORREKT: SUCHT NACH AspNetUserId >>
|
|
if (customerProfile == null)
|
|
{
|
|
customerProfile = new Webshop.Domain.Entities.Customer
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
AspNetUserId = customerUser.Id,
|
|
FirstName = "Test",
|
|
LastName = "Kunde"
|
|
};
|
|
context.Customers.Add(customerProfile);
|
|
await context.SaveChangesAsync();
|
|
Console.WriteLine("Customer profile created for new customer user.");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"Error creating customer user: {string.Join(", ", createCustomer.Errors.Select(e => e.Description))}");
|
|
}
|
|
}
|
|
// --- ENDE DES TEMPORÄREN SETUP-BLOCKS ---
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var logger = services.GetRequiredService<ILogger<Program>>();
|
|
logger.LogError(ex, "An error occurred during database migration or user initialization.");
|
|
}
|
|
} // <-- Hier endet der using-Scope
|
|
|
|
// --- START: HTTP REQUEST PIPELINE KONFIGURIEREN ---
|
|
|
|
// Middleware für Forwarded Headers (wichtig bei Reverse-Proxies wie Nginx oder Load Balancern)
|
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
|
{
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
|
});
|
|
|
|
// Swagger/SwaggerUI für API-Dokumentation aktivieren
|
|
// Für die Produktion wäre es sicherer, dies an `app.Environment.IsDevelopment()` zu binden
|
|
// if (app.Environment.IsDevelopment())
|
|
// {
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
// }
|
|
|
|
// app.UseHttpsRedirection(); // Auskommentiert für Docker HTTP-Entwicklung (da der Proxy HTTPS übernimmt)
|
|
|
|
// WICHTIG: Die Reihenfolge der Middleware ist entscheidend!
|
|
app.UseAuthentication(); // Zuerst prüfen, wer der Benutzer ist
|
|
app.UseAuthorization(); // Dann prüfen, was der Benutzer darf
|
|
|
|
app.MapControllers(); // Stellt sicher, dass Ihre Controller als Endpunkte gemappt werden
|
|
|
|
// --- ENDE: HTTP REQUEST PIPELINE KONFIGURIEREN ---
|
|
|
|
app.Run(); // Hier startet die Anwendung und blockiert die weitere Ausführung |