2026-03-01 02:13:26 +00:00
|
|
|
using IdentityModel;
|
2026-06-10 00:23:17 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2026-03-01 02:13:26 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-06-22 01:44:24 +01:00
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
|
|
using Yavsc;
|
2026-03-01 02:13:26 +00:00
|
|
|
using Yavsc.Interface;
|
2026-06-22 01:44:24 +01:00
|
|
|
using Yavsc.Interfaces;
|
2026-03-01 02:13:26 +00:00
|
|
|
using Yavsc.Models;
|
|
|
|
|
using Yavsc.Services;
|
2026-03-09 02:07:09 +00:00
|
|
|
using Yavsc.Server.Helpers;
|
2026-06-19 23:09:43 +01:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2026-06-10 16:59:23 +01:00
|
|
|
namespace Yavsc.Blogs;
|
2026-03-01 02:13:26 +00:00
|
|
|
|
|
|
|
|
internal class Program
|
|
|
|
|
{
|
|
|
|
|
private static async Task Main(string[] args)
|
2026-06-04 22:04:20 +01:00
|
|
|
{
|
|
|
|
|
Console.Title = "Yavsc.Blogs";
|
2026-03-09 02:07:09 +00:00
|
|
|
|
2026-06-04 22:04:20 +01:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
2026-06-15 00:33:38 +01:00
|
|
|
|
2026-06-15 00:11:48 +01:00
|
|
|
builder.AddConfiguration("blogs");
|
2026-03-09 02:07:09 +00:00
|
|
|
|
2026-06-19 13:15:21 +01:00
|
|
|
var services = builder.Services;
|
2026-03-09 02:07:09 +00:00
|
|
|
|
2026-06-19 23:09:43 +01:00
|
|
|
// MvcBuilder
|
|
|
|
|
builder.Services
|
2026-06-04 22:04:20 +01:00
|
|
|
.AddAuthorization(options =>
|
|
|
|
|
{
|
2026-06-06 21:30:41 +01:00
|
|
|
options.AddPolicy("BlogScope", policy =>
|
2026-06-04 22:04:20 +01:00
|
|
|
{
|
|
|
|
|
policy
|
2026-03-09 02:07:09 +00:00
|
|
|
.RequireAuthenticatedUser()
|
2026-06-19 23:09:43 +01:00
|
|
|
.RequireClaim(JwtClaimTypes.Scope, new string[] { "blog" });
|
2026-06-04 22:04:20 +01:00
|
|
|
});
|
|
|
|
|
})
|
2026-06-19 13:15:21 +01:00
|
|
|
.AddYavscCors(builder.Configuration)
|
2026-06-04 22:04:20 +01:00
|
|
|
.AddControllers();
|
|
|
|
|
|
2026-06-19 23:09:43 +01:00
|
|
|
// AuthenticationBuilder
|
2026-06-19 13:15:21 +01:00
|
|
|
services.AddAuthentication("Bearer")
|
|
|
|
|
.AddYavscJwtBearer(builder.Configuration);
|
2026-06-04 22:04:20 +01:00
|
|
|
|
2026-06-19 23:09:43 +01:00
|
|
|
// DbContextBuilder
|
2026-06-04 22:04:20 +01:00
|
|
|
services.AddDbContext<ApplicationDbContext>(options =>
|
2026-06-20 18:43:01 +01:00
|
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString(
|
|
|
|
|
YavscConstants.YavscConnectionStringName)));
|
2026-06-04 22:04:20 +01:00
|
|
|
|
2026-06-19 23:09:43 +01:00
|
|
|
// other services
|
|
|
|
|
services
|
|
|
|
|
.AddTransient<ITrueEmailSender, MailSender>()
|
|
|
|
|
.AddTransient<IEmailSender<ApplicationUser>, MailSender>()
|
2026-06-22 01:44:24 +01:00
|
|
|
.TryAddSingleton<ISmtpClientFactory, SmtpClientFactory>();
|
|
|
|
|
services
|
2026-06-19 23:09:43 +01:00
|
|
|
.AddTransient<IBillingService, BillingService>()
|
|
|
|
|
.AddTransient<ICalendarManager, CalendarManager>()
|
|
|
|
|
.AddTransient<IFileSystemAuthManager, FileSystemAuthManager>()
|
|
|
|
|
.AddTransient<BlogSpotService>()
|
|
|
|
|
.AddScoped<IAuthorizationHandler, PermissionHandler>()
|
|
|
|
|
.AddLocalization(options =>
|
2026-06-04 22:04:20 +01:00
|
|
|
{
|
|
|
|
|
options.ResourcesPath = "Resources";
|
2026-06-19 23:09:43 +01:00
|
|
|
})
|
|
|
|
|
.AddDistributedMemoryCache()
|
|
|
|
|
.AddSession(options =>
|
2026-06-04 22:04:20 +01:00
|
|
|
{
|
|
|
|
|
options.IdleTimeout = TimeSpan.FromMinutes(30);
|
|
|
|
|
options.Cookie.HttpOnly = true;
|
|
|
|
|
options.Cookie.IsEssential = false;
|
2026-06-19 23:09:43 +01:00
|
|
|
}).Configure<RequestLocalizationOptions>(options =>
|
2026-06-04 22:04:20 +01:00
|
|
|
{
|
|
|
|
|
var supportedCultures = new[] { "fr", "en", "pt" };
|
|
|
|
|
options.SetDefaultCulture(supportedCultures[0])
|
|
|
|
|
.AddSupportedCultures(supportedCultures)
|
|
|
|
|
.AddSupportedUICultures(supportedCultures);
|
|
|
|
|
});
|
2026-06-19 23:09:43 +01:00
|
|
|
|
|
|
|
|
// App startup
|
2026-06-04 22:04:20 +01:00
|
|
|
using (var app = builder.Build())
|
|
|
|
|
{
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
|
|
|
|
|
app
|
|
|
|
|
.UseRouting()
|
|
|
|
|
.UseAuthentication()
|
|
|
|
|
.UseAuthorization()
|
|
|
|
|
.UseCors("default")
|
|
|
|
|
;
|
2026-06-19 23:09:43 +01:00
|
|
|
app.MapIdentityApi<ApplicationUser>().RequireAuthorization("blog");
|
|
|
|
|
|
2026-06-04 22:04:20 +01:00
|
|
|
app.MapGet("/identity", (HttpContext context) =>
|
|
|
|
|
new JsonResult(context?.User?.Claims.Select(c => new { c.Type, c.Value }))
|
|
|
|
|
);
|
|
|
|
|
|
2026-06-22 01:44:24 +01:00
|
|
|
app.UseSession();
|
2026-06-04 22:04:20 +01:00
|
|
|
await app.RunAsync();
|
|
|
|
|
}
|
2026-03-01 02:13:26 +00:00
|
|
|
}
|
2026-03-09 02:07:09 +00:00
|
|
|
|
2026-03-01 02:13:26 +00:00
|
|
|
}
|