Add database layer (EF Core + Postgres) and Blazor Server admin site
- SOR.Database: 5 bounded-context DbContexts (identity, catalog, collection, bazaar, chat), all entities/enums, JSONB for card abilities, ServiceRegistration with per-context connection string fallback to "Default" - SOR.Admin: Blazor Server (.NET 8) — card editor, account management, bazaar overview, feature flags; Identity + cookie auth; FallbackPolicy requires Admin/SuperAdmin role; login via minimal API form POST endpoint - Directory.Packages.props: add Microsoft.AspNetCore.Identity.EntityFrameworkCore - SOR.slnx: add SOR.Admin + SOR.Database to solution (11 projects, 0 errors, 0 warnings) Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SOR.Admin;
|
||||
using SOR.Admin.Components;
|
||||
using SOR.Database;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Entities.Identity;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// ── Blazor Server ──────────────────────────────────────────────────────────
|
||||
builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
|
||||
// Required for <AuthorizeView> and [Authorize] in Blazor components.
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
builder.Services.AddScoped<AuthenticationStateProvider,
|
||||
IdentityRevalidatingAuthenticationStateProvider>();
|
||||
|
||||
// ── Identity + Cookie Auth ──────────────────────────────────────────────────
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultScheme = IdentityConstants.ApplicationScheme;
|
||||
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
|
||||
})
|
||||
.AddIdentityCookies();
|
||||
|
||||
// Redirect to /login instead of the default /Account/Login.
|
||||
builder.Services.ConfigureApplicationCookie(options =>
|
||||
{
|
||||
options.LoginPath = "/login";
|
||||
options.AccessDeniedPath = "/login";
|
||||
options.SlidingExpiration = true;
|
||||
options.ExpireTimeSpan = TimeSpan.FromHours(8);
|
||||
});
|
||||
|
||||
// All Blazor pages require Admin or SuperAdmin role by default.
|
||||
// Override per-page with [AllowAnonymous] (login page only).
|
||||
builder.Services.AddAuthorization(options =>
|
||||
{
|
||||
options.FallbackPolicy = new AuthorizationPolicyBuilder()
|
||||
.RequireAuthenticatedUser()
|
||||
.RequireRole("Admin", "SuperAdmin")
|
||||
.Build();
|
||||
});
|
||||
|
||||
builder.Services.AddIdentityCore<Account>(options =>
|
||||
{
|
||||
options.SignIn.RequireConfirmedAccount = false;
|
||||
options.Password.RequireDigit = true;
|
||||
options.Password.RequireUppercase = true;
|
||||
options.Password.RequireNonAlphanumeric = true;
|
||||
options.Password.RequiredLength = 12;
|
||||
options.Lockout.MaxFailedAccessAttempts = 5;
|
||||
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15);
|
||||
})
|
||||
.AddRoles<IdentityRole<Guid>>()
|
||||
.AddEntityFrameworkStores<SorIdentityDbContext>()
|
||||
.AddSignInManager()
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
// ── Razor Pages (used for login/logout form posts) ─────────────────────────
|
||||
builder.Services.AddRazorPages();
|
||||
|
||||
// ── Databases ─────────────────────────────────────────────────────────────
|
||||
builder.Services.AddIdentityDatabase(builder.Configuration);
|
||||
builder.Services.AddCatalogDatabase(builder.Configuration);
|
||||
builder.Services.AddCollectionDatabase(builder.Configuration);
|
||||
builder.Services.AddBazaarDatabase(builder.Configuration);
|
||||
builder.Services.AddChatDatabase(builder.Configuration);
|
||||
|
||||
// ── HTTP pipeline ─────────────────────────────────────────────────────────
|
||||
var app = builder.Build();
|
||||
|
||||
if (!app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseExceptionHandler("/Error");
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseStaticFiles();
|
||||
app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.UseAntiforgery();
|
||||
|
||||
// ── Auth endpoints (form POST targets — cookies cannot be set from Blazor) ──
|
||||
|
||||
// Login: called by the /login page form.
|
||||
app.MapPost("/auth/login", async (
|
||||
HttpContext ctx,
|
||||
[FromForm] string email,
|
||||
[FromForm] string password,
|
||||
[FromForm] string? returnUrl,
|
||||
UserManager<Account> userManager,
|
||||
SignInManager<Account> signInManager) =>
|
||||
{
|
||||
var user = await userManager.FindByEmailAsync(email);
|
||||
if (user is not null)
|
||||
{
|
||||
var result = await signInManager.PasswordSignInAsync(
|
||||
user, password, isPersistent: false, lockoutOnFailure: true);
|
||||
if (result.Succeeded)
|
||||
return Results.LocalRedirect(
|
||||
string.IsNullOrWhiteSpace(returnUrl) ? "/" : returnUrl);
|
||||
if (result.IsLockedOut)
|
||||
return Results.Redirect("/login?error=locked");
|
||||
}
|
||||
return Results.Redirect("/login?error=invalid");
|
||||
}).AllowAnonymous().DisableAntiforgery();
|
||||
|
||||
// Logout: called by the nav menu form.
|
||||
app.MapPost("/auth/logout", async (SignInManager<Account> signInManager) =>
|
||||
{
|
||||
await signInManager.SignOutAsync();
|
||||
return Results.Redirect("/login");
|
||||
}).DisableAntiforgery();
|
||||
|
||||
// ── Blazor + Razor Pages ──────────────────────────────────────────────────
|
||||
app.MapRazorPages();
|
||||
app.MapRazorComponents<App>()
|
||||
.AddInteractiveServerRenderMode();
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user