Files
pas/Api/Program.cs

68 lines
2.4 KiB
C#

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Abstractions;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.Resource;
namespace Api
{
using Microsoft.EntityFrameworkCore;
using Api.Models;
public static class Program
{
public static void Main(string[] args)
{
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 =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();
if (!app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
}
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}