Add simple auth
This commit is contained in:
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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"
|
@page "/events/create"
|
||||||
|
@attribute [Authorize]
|
||||||
@inject AppDbContext context
|
@inject AppDbContext context
|
||||||
@inject NavigationManager NavigationManager
|
@inject NavigationManager NavigationManager
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@page "/events/details"
|
@page "/events/details"
|
||||||
|
@attribute [Authorize]
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@inject AppDbContext context
|
@inject AppDbContext context
|
||||||
@inject NavigationManager NavigationManager
|
@inject NavigationManager NavigationManager
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@page "/events/edit"
|
@page "/events/edit"
|
||||||
|
@attribute [Authorize]
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@inject AppDbContext context
|
@inject AppDbContext context
|
||||||
@inject NavigationManager NavigationManager
|
@inject NavigationManager NavigationManager
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@page "/events"
|
@page "/events"
|
||||||
|
@attribute [Authorize]
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@using WebApp.Models
|
@using WebApp.Models
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
|
@attribute [Authorize]
|
||||||
@page "/events/printout"
|
@page "/events/printout"
|
||||||
@inject IConfiguration Configuration
|
@inject IConfiguration Configuration
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@page "/"
|
@page "/"
|
||||||
|
@attribute [Authorize]
|
||||||
@inject IConfiguration Configuration
|
@inject IConfiguration Configuration
|
||||||
|
|
||||||
<PageTitle>Home</PageTitle>
|
<PageTitle>Home</PageTitle>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@page "/import"
|
@page "/import"
|
||||||
|
@attribute [Authorize]
|
||||||
@using Core.Parsers
|
@using Core.Parsers
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
@using System.Text
|
@page "/meeting-schedule"
|
||||||
|
@attribute [Authorize]
|
||||||
|
@using System.Text
|
||||||
@using Core.Calculation
|
@using Core.Calculation
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@page "/meeting-schedule"
|
|
||||||
@inject IConfiguration Configuration
|
@inject IConfiguration Configuration
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
@inject ClipboardService ClipboardService
|
@inject ClipboardService ClipboardService
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@page "/students/create"
|
@page "/students/create"
|
||||||
|
@attribute [Authorize]
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
@inject NavigationManager NavigationManager
|
@inject NavigationManager NavigationManager
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@page "/students/details"
|
@page "/students/details"
|
||||||
|
@attribute [Authorize]
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@using Core.Entities
|
@using Core.Entities
|
||||||
@using Data
|
@using Data
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@page "/students/edit"
|
@page "/students/edit"
|
||||||
|
@attribute [Authorize]
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
@inject NavigationManager NavigationManager
|
@inject NavigationManager NavigationManager
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
@using Microsoft.EntityFrameworkCore
|
@page "/students/event-ranking"
|
||||||
|
@attribute [Authorize]
|
||||||
|
@using Microsoft.EntityFrameworkCore
|
||||||
@using WebApp.Models
|
@using WebApp.Models
|
||||||
@page "/students/event-ranking"
|
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
@rendermode InteractiveServer
|
@rendermode InteractiveServer
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
@using Microsoft.EntityFrameworkCore
|
@page "/students/event-ranking-edit/{StudentId:int}"
|
||||||
|
@attribute [Authorize]
|
||||||
|
@using Microsoft.EntityFrameworkCore
|
||||||
@using BlazorSortableList
|
@using BlazorSortableList
|
||||||
@using WebApp.Models
|
@using WebApp.Models
|
||||||
@page "/students/event-ranking-edit/{StudentId:int}"
|
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
@inject NavigationManager NavigationManager
|
@inject NavigationManager NavigationManager
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@page "/students"
|
@page "/students"
|
||||||
|
@attribute [Authorize]
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@using WebApp.Models
|
@using WebApp.Models
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@page "/teams/assignment"
|
@page "/teams/assignment"
|
||||||
|
@attribute [Authorize]
|
||||||
@using Core.Calculation
|
@using Core.Calculation
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@using WebApp.Models
|
@using WebApp.Models
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@page "/teams/create"
|
@page "/teams/create"
|
||||||
|
@attribute [Authorize]
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
@inject NavigationManager NavigationManager
|
@inject NavigationManager NavigationManager
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@page "/teams/edit"
|
@page "/teams/edit"
|
||||||
|
@attribute [Authorize]
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
@inject NavigationManager NavigationManager
|
@inject NavigationManager NavigationManager
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
@using Microsoft.EntityFrameworkCore
|
@page "/teams/handout"
|
||||||
|
@attribute [Authorize]
|
||||||
|
@using Microsoft.EntityFrameworkCore
|
||||||
@using WebApp.Models
|
@using WebApp.Models
|
||||||
@page "/teams/handout"
|
|
||||||
@inject IConfiguration Configuration
|
@inject IConfiguration Configuration
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
@page "/teams"
|
@using Microsoft.EntityFrameworkCore
|
||||||
@using Microsoft.EntityFrameworkCore
|
@page "/teams"
|
||||||
|
@attribute [Authorize]
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
@inject IDialogService DialogService
|
@inject IDialogService DialogService
|
||||||
@inject ISnackbar Snackbar
|
@inject ISnackbar Snackbar
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
@using Microsoft.EntityFrameworkCore
|
@page "/teams/printout"
|
||||||
|
@attribute [Authorize]
|
||||||
|
@using Microsoft.EntityFrameworkCore
|
||||||
@using WebApp.Models
|
@using WebApp.Models
|
||||||
@page "/teams/printout"
|
|
||||||
@inject IConfiguration Configuration
|
@inject IConfiguration Configuration
|
||||||
@inject AppDbContext Context
|
@inject AppDbContext Context
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,20 @@
|
|||||||
<Router AppAssembly="typeof(Program).Assembly">
|
@using WebApp.Components.Layout
|
||||||
|
@inject NavigationManager navigationManager
|
||||||
|
|
||||||
|
<Router AppAssembly="typeof(Program).Assembly">
|
||||||
<Found Context="routeData">
|
<Found Context="routeData">
|
||||||
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
|
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
|
||||||
<FocusOnNavigate RouteData="routeData" Selector="h1" />
|
<NotAuthorized>
|
||||||
|
@{
|
||||||
|
navigationManager.NavigateTo("/login", true);
|
||||||
|
}
|
||||||
|
</NotAuthorized>
|
||||||
|
</AuthorizeRouteView>
|
||||||
|
<FocusOnNavigate RouteData="@routeData" Selector="h1"/>
|
||||||
</Found>
|
</Found>
|
||||||
</Router>
|
<NotFound>
|
||||||
|
@{
|
||||||
|
navigationManager.NavigateTo("/login", true);
|
||||||
|
}
|
||||||
|
</NotFound>
|
||||||
|
</Router>
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
@using System.Net.Http
|
@using System.Net.Http
|
||||||
@using System.Net.Http.Json
|
@using System.Net.Http.Json
|
||||||
|
@using Microsoft.AspNetCore.Authorization
|
||||||
|
@using Microsoft.AspNetCore.Components.Authorization
|
||||||
@using Microsoft.AspNetCore.Components.Forms
|
@using Microsoft.AspNetCore.Components.Forms
|
||||||
@using Microsoft.AspNetCore.Components.Routing
|
@using Microsoft.AspNetCore.Components.Routing
|
||||||
@using Microsoft.AspNetCore.Components.Web
|
@using Microsoft.AspNetCore.Components.Web
|
||||||
|
|||||||
+21
-1
@@ -7,6 +7,7 @@ using WebApp.Components;
|
|||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
|
builder.Services.AddControllersWithViews();
|
||||||
builder.Services.AddRazorComponents()
|
builder.Services.AddRazorComponents()
|
||||||
.AddInteractiveServerComponents();
|
.AddInteractiveServerComponents();
|
||||||
|
|
||||||
@@ -25,6 +26,16 @@ builder.Services.AddScoped<ClipboardService>();
|
|||||||
builder.Services.AddScoped<StateContainer>(); // Server- side
|
builder.Services.AddScoped<StateContainer>(); // Server- side
|
||||||
builder.Services.AddSingleton<StateContainer>();//Client-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();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
@@ -36,7 +47,11 @@ if (!app.Environment.IsDevelopment())
|
|||||||
app.UseMigrationsEndPoint();
|
app.UseMigrationsEndPoint();
|
||||||
}
|
}
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
//app.UseHttpsRedirection();
|
||||||
|
app.UseRouting();
|
||||||
|
|
||||||
|
app.UseAuthentication();
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
app.UseAntiforgery();
|
app.UseAntiforgery();
|
||||||
@@ -44,4 +59,9 @@ app.UseAntiforgery();
|
|||||||
app.MapRazorComponents<App>()
|
app.MapRazorComponents<App>()
|
||||||
.AddInteractiveServerRenderMode();
|
.AddInteractiveServerRenderMode();
|
||||||
|
|
||||||
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapControllerRoute("default", "{controller}/{action}");
|
||||||
|
});
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="BlazorSortableList" Version="2.1.0" />
|
<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.Components.QuickGrid.EntityFrameworkAdapter" Version="9.0.8" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="9.0.8" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="9.0.8" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.8">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.8">
|
||||||
|
|||||||
Reference in New Issue
Block a user