113 lines
4.3 KiB
C#
113 lines
4.3 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();
|
|
}
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
var staticFilePath = "/workspaces/centrum/Web/dist/Web/browser";
|
|
app.UseDefaultFiles(new DefaultFilesOptions
|
|
{
|
|
FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(staticFilePath),
|
|
DefaultFileNames = new List<string> { "index.html" }
|
|
});
|
|
app.UseStaticFiles(new StaticFileOptions
|
|
{
|
|
FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(staticFilePath),
|
|
RequestPath = ""
|
|
});
|
|
// Angular routing fallback
|
|
app.Use(async (context, next) =>
|
|
{
|
|
await next();
|
|
var path = context.Request.Path.Value ?? string.Empty;
|
|
if (context.Response.StatusCode == 404 &&
|
|
!System.IO.Path.HasExtension(path) &&
|
|
!path.StartsWith("/api"))
|
|
{
|
|
context.Request.Path = "/index.html";
|
|
await next();
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
app.UseDefaultFiles(); // Uses wwwroot by default
|
|
app.UseStaticFiles();
|
|
// Angular routing fallback for production
|
|
app.Use(async (context, next) =>
|
|
{
|
|
await next();
|
|
var path = context.Request.Path.Value ?? string.Empty;
|
|
if (context.Response.StatusCode == 404 &&
|
|
!System.IO.Path.HasExtension(path) &&
|
|
!path.StartsWith("/api"))
|
|
{
|
|
context.Request.Path = "/index.html";
|
|
await next();
|
|
}
|
|
});
|
|
}
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|
|
}
|
|
}
|
|
}
|