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 <hongochai10@users.noreply.github.com>
This commit is contained in:
Cursor Agent
2026-02-26 19:26:51 +00:00
parent 2fcf73b33f
commit c789f964a8
10 changed files with 115 additions and 0 deletions

View File

@@ -19,6 +19,9 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- EN: JWT Bearer Authentication / VI: JWT Bearer Authentication -->
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.1" />
<!-- EN: Swagger/OpenAPI / VI: Swagger/OpenAPI -->
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />

View File

@@ -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 =>
{

View File

@@ -19,6 +19,9 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- EN: JWT Bearer Authentication / VI: JWT Bearer Authentication -->
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.1" />
<!-- EN: Swagger/OpenAPI / VI: Swagger/OpenAPI -->
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />

View File

@@ -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 =>
{

View File

@@ -19,6 +19,9 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- EN: JWT Bearer Authentication / VI: JWT Bearer Authentication -->
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.1" />
<!-- EN: Swagger/OpenAPI / VI: Swagger/OpenAPI -->
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />

View File

@@ -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 =>
{

View File

@@ -19,6 +19,9 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- EN: JWT Bearer Authentication / VI: JWT Bearer Authentication -->
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.1" />
<!-- EN: Swagger/OpenAPI / VI: Swagger/OpenAPI -->
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="7.2.0" />

View File

@@ -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 =>
{

View File

@@ -19,6 +19,9 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- EN: JWT Bearer Authentication / VI: JWT Bearer Authentication -->
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.1" />
<!-- EN: Swagger/OpenAPI / VI: Swagger/OpenAPI -->
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />

View File

@@ -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 =>
{