Projektdateien hinzufügen.
This commit is contained in:
87
Webshop.Api/Program.cs
Normal file
87
Webshop.Api/Program.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
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<55>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<75>gen
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
// --- ENDE: DIENSTE ZUM CONTAINER HINZUF<55>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<70>fen, wer der Benutzer ist (Authentifizierung)...
|
||||
app.UseAuthentication();
|
||||
// ...dann pr<70>fen, was der Benutzer darf (Autorisierung).
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
// --- ENDE: HTTP REQUEST PIPELINE KONFIGURIEREN ---
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user