feat: Add authentication configuration with OpenID Connect and update appsettings #4
This commit is contained in:
@@ -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
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user