Add simple auth

This commit is contained in:
2025-11-11 14:41:51 -05:00
parent 307c6e103f
commit d188d4fbd1
26 changed files with 121 additions and 17 deletions
+36
View File
@@ -0,0 +1,36 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
namespace WebApp.Authentication
{
public class AuthController : Controller
{
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> CookieLogin()
{
// Based on: https://www.codeproject.com/articles/Understanding-authentication-in-Blazor-and-ASP-NET
// TODO: Fix this up
// Generate the claims
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, "John Patton"));
claims.Add(new Claim(ClaimTypes.Role, "Contributor"));
var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, "Auth"));
await HttpContext.SignInAsync("Auth", principal).ConfigureAwait(false);
return Redirect("/");
}
[HttpPost]
public async Task<IActionResult> CookieLogout()
{
await HttpContext.SignOutAsync("Auth").ConfigureAwait(false);
return Redirect("/login");
}
}
}
+11
View File
@@ -0,0 +1,11 @@
@page "/login"
<h3>Login</h3>
<form action="Auth/CookieLogin" method="post">
<button type="submit" class="btn btn-primary">Login</button>
</form>
<form action="Auth/CookieLogout" method="post">
<button type="submit" class="btn btn-primary">Logout</button>
</form>
@@ -1,4 +1,5 @@
@page "/events/create"
@attribute [Authorize]
@inject AppDbContext context
@inject NavigationManager NavigationManager
@@ -1,4 +1,5 @@
@page "/events/details"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@inject AppDbContext context
@inject NavigationManager NavigationManager
@@ -1,4 +1,5 @@
@page "/events/edit"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@inject AppDbContext context
@inject NavigationManager NavigationManager
@@ -1,4 +1,5 @@
@page "/events"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@using WebApp.Models
@inject AppDbContext Context
@@ -1,4 +1,5 @@
@using Microsoft.EntityFrameworkCore
@attribute [Authorize]
@page "/events/printout"
@inject IConfiguration Configuration
@inject AppDbContext Context
+1
View File
@@ -1,4 +1,5 @@
@page "/"
@attribute [Authorize]
@inject IConfiguration Configuration
<PageTitle>Home</PageTitle>
+1
View File
@@ -1,4 +1,5 @@
@page "/import"
@attribute [Authorize]
@using Core.Parsers
@using Microsoft.EntityFrameworkCore
@inject AppDbContext Context
@@ -1,7 +1,8 @@
@using System.Text
@page "/meeting-schedule"
@attribute [Authorize]
@using System.Text
@using Core.Calculation
@using Microsoft.EntityFrameworkCore
@page "/meeting-schedule"
@inject IConfiguration Configuration
@inject AppDbContext Context
@inject ClipboardService ClipboardService
@@ -1,4 +1,5 @@
@page "/students/create"
@attribute [Authorize]
@inject AppDbContext Context
@inject NavigationManager NavigationManager
@@ -1,4 +1,5 @@
@page "/students/details"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@using Core.Entities
@using Data
@@ -1,4 +1,5 @@
@page "/students/edit"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@inject AppDbContext Context
@inject NavigationManager NavigationManager
@@ -1,6 +1,7 @@
@using Microsoft.EntityFrameworkCore
@page "/students/event-ranking"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@using WebApp.Models
@page "/students/event-ranking"
@inject AppDbContext Context
@rendermode InteractiveServer
@@ -1,7 +1,8 @@
@using Microsoft.EntityFrameworkCore
@page "/students/event-ranking-edit/{StudentId:int}"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@using BlazorSortableList
@using WebApp.Models
@page "/students/event-ranking-edit/{StudentId:int}"
@inject AppDbContext Context
@inject NavigationManager NavigationManager
@@ -1,4 +1,5 @@
@page "/students"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@using WebApp.Models
@inject AppDbContext Context
@@ -1,4 +1,5 @@
@page "/teams/assignment"
@attribute [Authorize]
@using Core.Calculation
@using Microsoft.EntityFrameworkCore
@using WebApp.Models
@@ -1,4 +1,5 @@
@page "/teams/create"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@inject AppDbContext Context
@inject NavigationManager NavigationManager
@@ -1,4 +1,5 @@
@page "/teams/edit"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@inject AppDbContext Context
@inject NavigationManager NavigationManager
@@ -1,6 +1,7 @@
@using Microsoft.EntityFrameworkCore
@page "/teams/handout"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@using WebApp.Models
@page "/teams/handout"
@inject IConfiguration Configuration
@inject AppDbContext Context
@@ -1,5 +1,6 @@
@page "/teams"
@using Microsoft.EntityFrameworkCore
@using Microsoft.EntityFrameworkCore
@page "/teams"
@attribute [Authorize]
@inject AppDbContext Context
@inject IDialogService DialogService
@inject ISnackbar Snackbar
@@ -1,6 +1,7 @@
@using Microsoft.EntityFrameworkCore
@page "/teams/printout"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@using WebApp.Models
@page "/teams/printout"
@inject IConfiguration Configuration
@inject AppDbContext Context
+18 -4
View File
@@ -1,6 +1,20 @@
<Router AppAssembly="typeof(Program).Assembly">
@using WebApp.Components.Layout
@inject NavigationManager navigationManager
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
<FocusOnNavigate RouteData="routeData" Selector="h1" />
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@{
navigationManager.NavigateTo("/login", true);
}
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1"/>
</Found>
</Router>
<NotFound>
@{
navigationManager.NavigateTo("/login", true);
}
</NotFound>
</Router>
+2
View File
@@ -1,5 +1,7 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
+21 -1
View File
@@ -7,6 +7,7 @@ using WebApp.Components;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
@@ -25,6 +26,16 @@ builder.Services.AddScoped<ClipboardService>();
builder.Services.AddScoped<StateContainer>(); // Server- side
builder.Services.AddSingleton<StateContainer>();//Client-side
// Add authentication options
builder.Services.AddAuthentication("Auth")
.AddCookie("Auth", options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
options.SlidingExpiration = true;
options.LoginPath = "/login";
});
builder.Services.AddCascadingAuthenticationState();
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -36,7 +47,11 @@ if (!app.Environment.IsDevelopment())
app.UseMigrationsEndPoint();
}
app.UseHttpsRedirection();
//app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseStaticFiles();
app.UseAntiforgery();
@@ -44,4 +59,9 @@ app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default", "{controller}/{action}");
});
app.Run();
+1
View File
@@ -13,6 +13,7 @@
<ItemGroup>
<PackageReference Include="BlazorSortableList" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="9.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Components.QuickGrid.EntityFrameworkAdapter" Version="9.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.8">