feat: update ProductController route and enhance authentication configuration

This commit is contained in:
Marek Lesko
2025-07-29 16:23:12 +00:00
parent 9217e805e9
commit 329fb94a3a
4 changed files with 26 additions and 24 deletions

View File

@@ -19,6 +19,7 @@ namespace Api.Controllers
// GET: api/Product // GET: api/Product
[HttpGet] [HttpGet]
[Route("api/product")]
public async Task<ActionResult<IEnumerable<Product>>> GetProducts([FromQuery] int? id = null) public async Task<ActionResult<IEnumerable<Product>>> GetProducts([FromQuery] int? id = null)
{ {
if (id.HasValue) if (id.HasValue)

View File

@@ -22,17 +22,17 @@ namespace Api
}) })
.AddJwtBearer(options => .AddJwtBearer(options =>
{ {
options.Events = new JwtBearerEvents // options.Events = new JwtBearerEvents
{ // {
OnTokenValidated = context => Task.CompletedTask, // OnTokenValidated = context => Task.CompletedTask,
OnChallenge = context => Task.CompletedTask // OnChallenge = context => Task.CompletedTask
}; // };
options.Authority = builder.Configuration.GetConnectionString("Authentication:PocketId:Authority"); options.Authority = builder.Configuration["Authentication:PocketId:Authority"];
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters() options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{ {
// ValidAudiences = builder.Configuration.GetSection("Authentication:PocketId:Audiences").Get<string[]>(), ValidAudiences = builder.Configuration["Authentication:PocketId:ClientId"].Split(';').Select(i => i.Trim()).ToArray(),
ValidIssuers = builder.Configuration.GetSection("Authentication:PocketId:Authority").Get<string[]>() ValidIssuers = builder.Configuration["Authentication:PocketId:Authority"].Split(';').Select(i => i.Trim()).ToArray()
}; };
}); });
@@ -65,19 +65,19 @@ namespace Api
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
app.Use(async (context, next) => // app.Use(async (context, next) =>
{ // {
if (context.Request.Method == HttpMethods.Options) // if (context.Request.Method == HttpMethods.Options)
{ // {
context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); // context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); // context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
context.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type"); // context.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type");
context.Response.StatusCode = StatusCodes.Status204NoContent; // context.Response.StatusCode = StatusCodes.Status204NoContent;
return; // return;
} // }
await next(); // await next();
}); // });
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {
app.UseHttpsRedirection(); app.UseHttpsRedirection();

View File

@@ -14,5 +14,5 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "http://localhost:4200" "AllowedHosts": "*"
} }

View File

@@ -1,4 +1,4 @@
import { HttpClient } from '@angular/common/http'; import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc'; import { OAuthService } from 'angular-oauth2-oidc';
@@ -13,8 +13,9 @@ export class Login implements OnInit {
} }
ngOnInit(): void { ngOnInit(): void {
this.httpClient.get('http://localhost:5000/api/product' this.httpClient.get('http://localhost:5000/swagger/v1/swagger.json', {
// { headers: { Authorization: `Bearer ${this.as.getAccessToken()}` } } headers: new HttpHeaders({ Authorization: `Bearer ${this.as.getAccessToken()}` }).append('Content-Type', 'application/json')
}
).subscribe(console.warn); ).subscribe(console.warn);
} }
} }