87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Text;
|
|
using Webshop.Application.Services;
|
|
using Webshop.Domain.Interfaces;
|
|
using Webshop.Infrastructure.Data;
|
|
using Webshop.Infrastructure.Repositories;
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// --- START: DIENSTE ZUM CONTAINER HINZUFÜGEN ---
|
|
|
|
// 1. Datenbank-Kontext (DbContext) registrieren
|
|
// Sagt der Anwendung, wie sie sich mit der PostgreSQL-Datenbank verbinden soll.
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
// 2. ASP.NET Core Identity für Benutzerverwaltung registrieren
|
|
// Verwendet unseren DbContext, um Benutzer- und Rollen-Daten zu speichern.
|
|
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
|
.AddDefaultTokenProviders();
|
|
|
|
// 3. JWT-Authentifizierung konfigurieren
|
|
// Richtet ein, wie die API die von Benutzern gesendeten Tokens validieren soll.
|
|
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
|
|
var secretKey = jwtSettings["Secret"] ?? throw new InvalidOperationException("JWT Secret not found");
|
|
|
|
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))
|
|
};
|
|
});
|
|
|
|
// 4. Unsere eigenen Interfaces und Klassen registrieren (Dependency Injection)
|
|
// Sagt: "Immer wenn jemand nach 'IProductRepository' fragt, gib ihm eine neue 'ProductRepository'-Instanz."
|
|
builder.Services.AddScoped<IProductRepository, ProductRepository>();
|
|
|
|
# region Services
|
|
builder.Services.AddScoped<ProductService>();
|
|
# endregion
|
|
// 5. Controller und Swagger/OpenAPI hinzufügen
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
// --- ENDE: DIENSTE ZUM CONTAINER HINZUFÜGEN ---
|
|
|
|
var app = builder.Build();
|
|
|
|
// --- START: HTTP REQUEST PIPELINE KONFIGURIEREN ---
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
// WICHTIG: Die Reihenfolge ist entscheidend!
|
|
// Zuerst prüfen, wer der Benutzer ist (Authentifizierung)...
|
|
app.UseAuthentication();
|
|
// ...dann prüfen, was der Benutzer darf (Autorisierung).
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
// --- ENDE: HTTP REQUEST PIPELINE KONFIGURIEREN ---
|
|
|
|
app.Run(); |