From c789f964a82d0009a086730121e57dee79d31fe7 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 26 Feb 2026 19:26:51 +0000 Subject: [PATCH] Add JWT Bearer authentication registration to 5 microservice Program.cs files Add AddAuthentication(JwtBearerDefaults.AuthenticationScheme) and AddJwtBearer() service registration before CORS configuration in: - CatalogService.API - OrderService.API - InventoryService.API - FnbEngine.API - BookingService.API Also add Microsoft.AspNetCore.Authentication.JwtBearer v10.0.1 NuGet package reference to each service's .csproj file. This fixes the runtime error caused by UseAuthentication() being called without a registered authentication scheme. Co-authored-by: Velik --- .../BookingService.API.csproj | 3 +++ .../src/BookingService.API/Program.cs | 20 +++++++++++++++++++ .../CatalogService.API.csproj | 3 +++ .../src/CatalogService.API/Program.cs | 20 +++++++++++++++++++ .../src/FnbEngine.API/FnbEngine.API.csproj | 3 +++ .../src/FnbEngine.API/Program.cs | 20 +++++++++++++++++++ .../InventoryService.API.csproj | 3 +++ .../src/InventoryService.API/Program.cs | 20 +++++++++++++++++++ .../OrderService.API/OrderService.API.csproj | 3 +++ .../src/OrderService.API/Program.cs | 20 +++++++++++++++++++ 10 files changed, 115 insertions(+) diff --git a/services/booking-service-net/src/BookingService.API/BookingService.API.csproj b/services/booking-service-net/src/BookingService.API/BookingService.API.csproj index 4fa3b98c..2b544b3f 100644 --- a/services/booking-service-net/src/BookingService.API/BookingService.API.csproj +++ b/services/booking-service-net/src/BookingService.API/BookingService.API.csproj @@ -19,6 +19,9 @@ all + + + diff --git a/services/booking-service-net/src/BookingService.API/Program.cs b/services/booking-service-net/src/BookingService.API/Program.cs index 6bb7c1e5..54a508e3 100644 --- a/services/booking-service-net/src/BookingService.API/Program.cs +++ b/services/booking-service-net/src/BookingService.API/Program.cs @@ -85,6 +85,26 @@ try name: "postgresql", tags: ["db", "postgresql"]); + // EN: Add JWT Bearer authentication / VI: Thêm JWT Bearer authentication + var jwtAuthority = builder.Configuration["Jwt:Authority"] ?? "http://localhost:5001"; + var jwtSecret = builder.Configuration["Jwt:Secret"] ?? ""; + builder.Services.AddAuthentication(Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(options => + { + options.RequireHttpsMetadata = false; + options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters + { + ValidateIssuer = false, + ValidateAudience = false, + ValidateLifetime = true, + ValidateIssuerSigningKey = !string.IsNullOrEmpty(jwtSecret), + IssuerSigningKey = !string.IsNullOrEmpty(jwtSecret) + ? new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtSecret)) + : null, + }; + }); + builder.Services.AddAuthorization(); + // EN: Add CORS / VI: Thêm CORS builder.Services.AddCors(options => { diff --git a/services/catalog-service-net/src/CatalogService.API/CatalogService.API.csproj b/services/catalog-service-net/src/CatalogService.API/CatalogService.API.csproj index 6cea7eab..4a6d39e1 100644 --- a/services/catalog-service-net/src/CatalogService.API/CatalogService.API.csproj +++ b/services/catalog-service-net/src/CatalogService.API/CatalogService.API.csproj @@ -19,6 +19,9 @@ all + + + diff --git a/services/catalog-service-net/src/CatalogService.API/Program.cs b/services/catalog-service-net/src/CatalogService.API/Program.cs index 54f836fd..845cea94 100644 --- a/services/catalog-service-net/src/CatalogService.API/Program.cs +++ b/services/catalog-service-net/src/CatalogService.API/Program.cs @@ -85,6 +85,26 @@ try name: "postgresql", tags: ["db", "postgresql"]); + // EN: Add JWT Bearer authentication / VI: Thêm JWT Bearer authentication + var jwtAuthority = builder.Configuration["Jwt:Authority"] ?? "http://localhost:5001"; + var jwtSecret = builder.Configuration["Jwt:Secret"] ?? ""; + builder.Services.AddAuthentication(Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(options => + { + options.RequireHttpsMetadata = false; + options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters + { + ValidateIssuer = false, + ValidateAudience = false, + ValidateLifetime = true, + ValidateIssuerSigningKey = !string.IsNullOrEmpty(jwtSecret), + IssuerSigningKey = !string.IsNullOrEmpty(jwtSecret) + ? new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtSecret)) + : null, + }; + }); + builder.Services.AddAuthorization(); + // EN: Add CORS / VI: Thêm CORS builder.Services.AddCors(options => { diff --git a/services/fnb-engine-net/src/FnbEngine.API/FnbEngine.API.csproj b/services/fnb-engine-net/src/FnbEngine.API/FnbEngine.API.csproj index 583c6ec9..9be4c7bf 100644 --- a/services/fnb-engine-net/src/FnbEngine.API/FnbEngine.API.csproj +++ b/services/fnb-engine-net/src/FnbEngine.API/FnbEngine.API.csproj @@ -19,6 +19,9 @@ all + + + diff --git a/services/fnb-engine-net/src/FnbEngine.API/Program.cs b/services/fnb-engine-net/src/FnbEngine.API/Program.cs index c3d42f22..cb136a71 100644 --- a/services/fnb-engine-net/src/FnbEngine.API/Program.cs +++ b/services/fnb-engine-net/src/FnbEngine.API/Program.cs @@ -85,6 +85,26 @@ try name: "postgresql", tags: ["db", "postgresql"]); + // EN: Add JWT Bearer authentication / VI: Thêm JWT Bearer authentication + var jwtAuthority = builder.Configuration["Jwt:Authority"] ?? "http://localhost:5001"; + var jwtSecret = builder.Configuration["Jwt:Secret"] ?? ""; + builder.Services.AddAuthentication(Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(options => + { + options.RequireHttpsMetadata = false; + options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters + { + ValidateIssuer = false, + ValidateAudience = false, + ValidateLifetime = true, + ValidateIssuerSigningKey = !string.IsNullOrEmpty(jwtSecret), + IssuerSigningKey = !string.IsNullOrEmpty(jwtSecret) + ? new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtSecret)) + : null, + }; + }); + builder.Services.AddAuthorization(); + // EN: Add CORS / VI: Thêm CORS builder.Services.AddCors(options => { diff --git a/services/inventory-service-net/src/InventoryService.API/InventoryService.API.csproj b/services/inventory-service-net/src/InventoryService.API/InventoryService.API.csproj index c7ca418a..d6601e31 100644 --- a/services/inventory-service-net/src/InventoryService.API/InventoryService.API.csproj +++ b/services/inventory-service-net/src/InventoryService.API/InventoryService.API.csproj @@ -19,6 +19,9 @@ all + + + diff --git a/services/inventory-service-net/src/InventoryService.API/Program.cs b/services/inventory-service-net/src/InventoryService.API/Program.cs index 8790a8a4..739ac466 100644 --- a/services/inventory-service-net/src/InventoryService.API/Program.cs +++ b/services/inventory-service-net/src/InventoryService.API/Program.cs @@ -88,6 +88,26 @@ try name: "postgresql", tags: ["db", "postgresql"]); + // EN: Add JWT Bearer authentication / VI: Thêm JWT Bearer authentication + var jwtAuthority = builder.Configuration["Jwt:Authority"] ?? "http://localhost:5001"; + var jwtSecret = builder.Configuration["Jwt:Secret"] ?? ""; + builder.Services.AddAuthentication(Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(options => + { + options.RequireHttpsMetadata = false; + options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters + { + ValidateIssuer = false, + ValidateAudience = false, + ValidateLifetime = true, + ValidateIssuerSigningKey = !string.IsNullOrEmpty(jwtSecret), + IssuerSigningKey = !string.IsNullOrEmpty(jwtSecret) + ? new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtSecret)) + : null, + }; + }); + builder.Services.AddAuthorization(); + // EN: Add CORS / VI: Thêm CORS builder.Services.AddCors(options => { diff --git a/services/order-service-net/src/OrderService.API/OrderService.API.csproj b/services/order-service-net/src/OrderService.API/OrderService.API.csproj index 50dcdef1..c76483fd 100644 --- a/services/order-service-net/src/OrderService.API/OrderService.API.csproj +++ b/services/order-service-net/src/OrderService.API/OrderService.API.csproj @@ -19,6 +19,9 @@ all + + + diff --git a/services/order-service-net/src/OrderService.API/Program.cs b/services/order-service-net/src/OrderService.API/Program.cs index 7e44d281..64bf61af 100644 --- a/services/order-service-net/src/OrderService.API/Program.cs +++ b/services/order-service-net/src/OrderService.API/Program.cs @@ -148,6 +148,26 @@ try name: "postgresql", tags: ["db", "postgresql"]); + // EN: Add JWT Bearer authentication / VI: Thêm JWT Bearer authentication + var jwtAuthority = builder.Configuration["Jwt:Authority"] ?? "http://localhost:5001"; + var jwtSecret = builder.Configuration["Jwt:Secret"] ?? ""; + builder.Services.AddAuthentication(Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(options => + { + options.RequireHttpsMetadata = false; + options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters + { + ValidateIssuer = false, + ValidateAudience = false, + ValidateLifetime = true, + ValidateIssuerSigningKey = !string.IsNullOrEmpty(jwtSecret), + IssuerSigningKey = !string.IsNullOrEmpty(jwtSecret) + ? new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtSecret)) + : null, + }; + }); + builder.Services.AddAuthorization(); + // EN: Add CORS / VI: Thêm CORS builder.Services.AddCors(options => {