Auth added to blazor
This commit is contained in:
@@ -1,10 +1,30 @@
|
|||||||
<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
|
@inject NavigationManager NavigationManager
|
||||||
<Found Context="routeData">
|
|
||||||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
|
<CascadingAuthenticationState>
|
||||||
</Found>
|
<Router AppAssembly="@typeof(Program).Assembly">
|
||||||
<NotFound>
|
<Found Context="routeData">
|
||||||
<LayoutView Layout="@typeof(MainLayout)">
|
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
|
||||||
<p>Sorry, there's nothing at this address.</p>
|
<NotAuthorized>
|
||||||
</LayoutView>
|
@{
|
||||||
</NotFound>
|
var returnUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
|
||||||
</Router>
|
|
||||||
|
NavigationManager.NavigateTo($"login?redirectUri={returnUrl}", forceLoad: true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
</NotAuthorized>
|
||||||
|
<Authorizing>
|
||||||
|
Wait...
|
||||||
|
</Authorizing>
|
||||||
|
</AuthorizeRouteView>
|
||||||
|
</Found>
|
||||||
|
<NotFound>
|
||||||
|
|
||||||
|
<LayoutView Layout="@typeof(MainLayout)">
|
||||||
|
<p>Sorry, there's nothing at this address.</p>
|
||||||
|
</LayoutView>
|
||||||
|
|
||||||
|
</NotFound>
|
||||||
|
|
||||||
|
</Router>
|
||||||
|
</CascadingAuthenticationState>
|
||||||
19
Web/Pages/Login.cshtml
Normal file
19
Web/Pages/Login.cshtml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
@page
|
||||||
|
@model Web.Pages.Login
|
||||||
|
|
||||||
|
@{
|
||||||
|
Layout = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
20
Web/Pages/Login.cshtml.cs
Normal file
20
Web/Pages/Login.cshtml.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Authentication;
|
||||||
|
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
|
||||||
|
namespace Web.Pages
|
||||||
|
{
|
||||||
|
public class Login : PageModel
|
||||||
|
{
|
||||||
|
// Can't be put into a razor page
|
||||||
|
public async Task OnGet(string redirectUri)
|
||||||
|
{
|
||||||
|
await HttpContext.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties()
|
||||||
|
{
|
||||||
|
RedirectUri = redirectUri
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
Web/Pages/Logout.cshtml
Normal file
19
Web/Pages/Logout.cshtml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
@page
|
||||||
|
@model Web.Pages.Logout
|
||||||
|
|
||||||
|
@{
|
||||||
|
Layout = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
16
Web/Pages/Logout.cshtml.cs
Normal file
16
Web/Pages/Logout.cshtml.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Authentication;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
|
||||||
|
namespace Web.Pages
|
||||||
|
{
|
||||||
|
public class Logout : PageModel
|
||||||
|
{
|
||||||
|
public async Task<IActionResult> OnGet()
|
||||||
|
{
|
||||||
|
await HttpContext.SignOutAsync();
|
||||||
|
return Redirect("/");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
Web/Shared/LoginDisplay.razor
Normal file
9
Web/Shared/LoginDisplay.razor
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<AuthorizeView>
|
||||||
|
<Authorized>
|
||||||
|
Hello, @(context.User.Identity.Name)!
|
||||||
|
<a href="logout">Log out</a>
|
||||||
|
</Authorized>
|
||||||
|
<NotAuthorized>
|
||||||
|
<a href="login?redirectUri=/">Log in</a>
|
||||||
|
</NotAuthorized>
|
||||||
|
</AuthorizeView>
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
<div class="main">
|
<div class="main">
|
||||||
<div class="top-row px-4">
|
<div class="top-row px-4">
|
||||||
|
<LoginDisplay/>
|
||||||
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
|
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||||
|
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Components;
|
using Microsoft.AspNetCore.Components;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
@@ -9,6 +11,8 @@ using Microsoft.AspNetCore.HttpsPolicy;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using Web.Data;
|
using Web.Data;
|
||||||
|
|
||||||
namespace Web
|
namespace Web
|
||||||
@@ -28,17 +32,40 @@ namespace Web
|
|||||||
{
|
{
|
||||||
services.AddRazorPages();
|
services.AddRazorPages();
|
||||||
services.AddServerSideBlazor();
|
services.AddServerSideBlazor();
|
||||||
|
services.AddHttpContextAccessor();
|
||||||
services.AddSingleton<WeatherForecastService>();
|
services.AddSingleton<WeatherForecastService>();
|
||||||
services.AddAuthentication(options =>
|
services.AddAuthentication(options =>
|
||||||
{
|
{
|
||||||
options.DefaultScheme = "Cookies";
|
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
||||||
options.DefaultChallengeScheme = "oidc";
|
options.DefaultChallengeScheme = "oidc";
|
||||||
})
|
})
|
||||||
.AddCookie("Cookies")
|
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
|
||||||
.AddOpenIdConnect(options =>
|
.AddOpenIdConnect(options =>
|
||||||
{
|
{
|
||||||
options.Authority = "https://authentik.mattstop.com/application/o/petrie-panel/";
|
options.Authority = Configuration["oidc:authority"];
|
||||||
options.ClientId = "85bcc426a47ac2c3575b6d590ec4f53db237e212";
|
options.ClientId = Configuration["oidc:client_id"];
|
||||||
|
options.ClientSecret = Configuration["oidc:client_secret"];
|
||||||
|
|
||||||
|
options.ResponseType = OpenIdConnectResponseType.Code;
|
||||||
|
options.GetClaimsFromUserInfoEndpoint = true;
|
||||||
|
options.SaveTokens = true;
|
||||||
|
options.Scope.Add("openid");
|
||||||
|
options.Scope.Add("profile");
|
||||||
|
options.Scope.Add("email");
|
||||||
|
options.TokenValidationParameters = new
|
||||||
|
TokenValidationParameters
|
||||||
|
{
|
||||||
|
NameClaimType = "name"
|
||||||
|
};
|
||||||
|
options.Events = new OpenIdConnectEvents
|
||||||
|
{
|
||||||
|
OnAccessDenied = context =>
|
||||||
|
{
|
||||||
|
context.HandleResponse();
|
||||||
|
context.Response.Redirect("/");
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +87,8 @@ namespace Web
|
|||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
|
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
//app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.UseEndpoints(endpoints =>
|
app.UseEndpoints(endpoints =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,5 +6,10 @@
|
|||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*",
|
||||||
|
"oidc" : {
|
||||||
|
"authority" : "https://dummy.dummy",
|
||||||
|
"client_id": "id",
|
||||||
|
"client_secret": "secret"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user