feat: Add authentication configuration with OpenID Connect and update appsettings #4

This commit is contained in:
Marek Lesko
2025-07-27 16:30:17 +00:00
parent 813b7ac9aa
commit b991f009c7
3 changed files with 37 additions and 0 deletions

View File

@@ -1,10 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Api.Models;
using Microsoft.AspNetCore.Authorization;
namespace Api.Controllers
{
[ApiController]
[Authorize]
[Route("api/[controller]")]
public class ProductController : ControllerBase
{

View File

@@ -15,6 +15,28 @@ namespace Api
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
var pocketId = builder.Configuration.GetSection("Authentication:PocketId");
options.Authority = pocketId["Authority"];
options.ClientId = pocketId["ClientId"];
options.ClientSecret = pocketId["ClientSecret"];
options.CallbackPath = pocketId["CallbackPath"];
options.ResponseType = "code";
options.SaveTokens = true;
options.Scope.Clear();
var scopes = pocketId["Scopes"] ?? "openid";
foreach (var scope in scopes.Split(' '))
{
options.Scope.Add(scope);
}
});
builder.Services.AddControllers();
// Add DbContext with SQL Server
builder.Services.AddDbContext<AppDbContext>(options =>
@@ -34,6 +56,9 @@ namespace Api
app.UseHttpsRedirection();
}
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -8,4 +8,14 @@
"ConnectionStrings": {
"DefaultConnection": "Server=localhost,1433;Database=CentrumDb;User=sa;Password=P@ssw0rd;TrustServerCertificate=True;"
}
,
"Authentication": {
"PocketId": {
"Authority": "https://identity.lesko.me",
"ClientId": "21131567-fea1-42a2-8907-21abd874eff8",
"ClientSecret": "a633GE6G3JoY8WopnsxhSXQpmsTuXa63",
"CallbackPath": "/signin-pocketid",
"Scopes": "openid profile email"
}
}
}