Remove Blazor project setup, including authentication, routing, and default configurations.
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
@page "/auth"
|
||||
|
||||
@using Microsoft.AspNetCore.Authorization
|
||||
|
||||
@attribute [Authorize]
|
||||
@rendermode InteractiveAuto
|
||||
|
||||
<PageTitle>Auth</PageTitle>
|
||||
|
||||
<h1>You are authenticated</h1>
|
||||
|
||||
<AuthorizeView>
|
||||
Hello @context.User.Identity?.Name!
|
||||
</AuthorizeView>
|
||||
@@ -0,0 +1,19 @@
|
||||
@page "/counter"
|
||||
@rendermode InteractiveAuto
|
||||
|
||||
<PageTitle>Counter</PageTitle>
|
||||
|
||||
<h1>Counter</h1>
|
||||
|
||||
<p role="status">Current count: @currentCount</p>
|
||||
|
||||
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
|
||||
|
||||
@code {
|
||||
private int currentCount = 0;
|
||||
|
||||
private void IncrementCount()
|
||||
{
|
||||
currentCount++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
|
||||
namespace Polycomm.Client;
|
||||
|
||||
// This is a client-side AuthenticationStateProvider that determines the user's authentication state by
|
||||
// looking for data persisted in the page when it was rendered on the server. This authentication state will
|
||||
// be fixed for the lifetime of the WebAssembly application. So, if the user needs to log in or out, a full
|
||||
// page reload is required.
|
||||
//
|
||||
// This only provides a user name and email for display purposes. It does not actually include any tokens
|
||||
// that authenticate to the server when making subsequent requests. That works separately using a
|
||||
// cookie that will be included on HttpClient requests to the server.
|
||||
internal class PersistentAuthenticationStateProvider : AuthenticationStateProvider
|
||||
{
|
||||
private static readonly Task<AuthenticationState> defaultUnauthenticatedTask =
|
||||
Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
|
||||
|
||||
private readonly Task<AuthenticationState> authenticationStateTask = defaultUnauthenticatedTask;
|
||||
|
||||
public PersistentAuthenticationStateProvider(PersistentComponentState state)
|
||||
{
|
||||
if (!state.TryTakeFromJson<UserInfo>(nameof(UserInfo), out var userInfo) || userInfo is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Claim[] claims = [
|
||||
new Claim(ClaimTypes.NameIdentifier, userInfo.UserId),
|
||||
new Claim(ClaimTypes.Name, userInfo.Email),
|
||||
new Claim(ClaimTypes.Email, userInfo.Email) ];
|
||||
|
||||
authenticationStateTask = Task.FromResult(
|
||||
new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(claims,
|
||||
authenticationType: nameof(PersistentAuthenticationStateProvider)))));
|
||||
}
|
||||
|
||||
public override Task<AuthenticationState> GetAuthenticationStateAsync() => authenticationStateTask;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
|
||||
<StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.27" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.27" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,11 @@
|
||||
using Polycomm.Client;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
|
||||
var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
||||
|
||||
builder.Services.AddAuthorizationCore();
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
builder.Services.AddSingleton<AuthenticationStateProvider, PersistentAuthenticationStateProvider>();
|
||||
|
||||
await builder.Build().RunAsync();
|
||||
@@ -0,0 +1,8 @@
|
||||
@inject NavigationManager NavigationManager
|
||||
|
||||
@code {
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
NavigationManager.NavigateTo($"Account/Login?returnUrl={Uri.EscapeDataString(NavigationManager.Uri)}", forceLoad: true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Polycomm.Client;
|
||||
|
||||
// Add properties to this class and update the server and client AuthenticationStateProviders
|
||||
// to expose more information about the authenticated user to the client.
|
||||
public class UserInfo
|
||||
{
|
||||
public required string UserId { get; set; }
|
||||
public required string Email { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
@using System.Net.Http
|
||||
@using System.Net.Http.Json
|
||||
@using Microsoft.AspNetCore.Components.Authorization
|
||||
@using Microsoft.AspNetCore.Components.Forms
|
||||
@using Microsoft.AspNetCore.Components.Routing
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using static Microsoft.AspNetCore.Components.Web.RenderMode
|
||||
@using Microsoft.AspNetCore.Components.Web.Virtualization
|
||||
@using Microsoft.JSInterop
|
||||
@using Polycomm.Client
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user