Skip to content

Commit

Permalink
Merge branch 'net8' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Dec 7, 2023
2 parents 7215993 + 8fe8c80 commit a197582
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
</PropertyGroup>

<ItemGroup>

<ProjectReference Include="..\..\..\src\Sitko.Core.Blazor.MudBlazor\Sitko.Core.Blazor.MudBlazor.csproj" />
<ProjectReference Include="..\..\..\src\Sitko.Core.Blazor.Wasm\Sitko.Core.Blazor.Wasm.csproj" />
<ProjectReference Include="..\..\..\src\Sitko.Core.Repository.Remote.Wasm\Sitko.Core.Repository.Remote.Wasm.csproj" />
<ProjectReference Include="..\..\..\src\Sitko.Core.Storage.Remote\Sitko.Core.Storage.Remote.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
<ProjectReference Include="..\MudBlazorAuto.Data\MudBlazorAuto.Data.csproj" />
</ItemGroup>

Expand Down
3 changes: 2 additions & 1 deletion apps/Blazor/MudBlazorAuto.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
.AddHttpClient(nameof(HttpRepositoryTransport)).AddHttpMessageHandler<CookieHandler>();
builder.Services.AddScoped(_ => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();
builder.ConfigureLocalization("ru-RU");
await builder.RunApplicationAsync();

public class TestRemoteStorageOptions : StorageOptions, IRemoteStorageOptions
{
Expand Down
25 changes: 3 additions & 22 deletions apps/Blazor/MudBlazorAuto/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
builder
.AddSitkoCoreBlazorServer()
.AddMudBlazorServer()
.AddInteractiveWebAssembly()
.AddPostgresDatabase<BarContext>(options =>
{
options.EnableSensitiveLogging = true;
Expand All @@ -46,30 +47,10 @@
builder.Services.AddControllers();
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();
app.UseAntiforgery();
app.ConfigureLocalization("ru-RU");

app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode()
app.MapSitkoCoreBlazor<App>()
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof(Index).Assembly);

app.ConfigureLocalization("ru-RU");

app.MapControllers();

app.Run();
5 changes: 4 additions & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Localization.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Localization" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
Expand All @@ -141,6 +142,8 @@
<PackageVersion Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Components" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="8.0.0" />
Expand All @@ -150,4 +153,4 @@
<PackageVersion Include="AspNetCore.HealthChecks.OpenIdConnectServer" Version="7.0.0" />
<PackageVersion Include="AspNetCore.HealthChecks.Hangfire" Version="7.0.0" />
</ItemGroup>
</Project>
</Project>
34 changes: 33 additions & 1 deletion src/Sitko.Core.Blazor.Server/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using JetBrains.Annotations;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Sitko.Core.App;
using Sitko.Core.App.Web;

namespace Sitko.Core.Blazor.Server;

Expand All @@ -15,4 +18,33 @@ public static ISitkoCoreBlazorServerApplicationBuilder AddSitkoCoreBlazorServer(
this WebApplicationBuilder webApplicationBuilder, string[] args) =>
ApplicationBuilderFactory.GetOrCreateApplicationBuilder(webApplicationBuilder,
applicationBuilder => new SitkoCoreBlazorServerApplicationBuilder(applicationBuilder, args));
}

public static ISitkoCoreBlazorApplicationBuilder AddInteractiveWebAssembly(
this ISitkoCoreBlazorApplicationBuilder builder)
{
builder.ConfigureServices(services =>
{
services.AddRazorComponents().AddInteractiveWebAssemblyComponents();
});
return builder;
}

public static ISitkoCoreBlazorApplicationBuilder
AddWebAssemblyAuth<TAuthenticationStateProvider>(this ISitkoCoreBlazorApplicationBuilder builder)
where TAuthenticationStateProvider : AuthenticationStateProvider
{
builder.ConfigureServices(services =>
{
services.AddCascadingAuthenticationState();
services.AddScoped<AuthenticationStateProvider, TAuthenticationStateProvider>();
});
return builder;
}

public static RazorComponentsEndpointConventionBuilder MapSitkoCoreBlazor<TRootComponent>(this WebApplication app)
{
app.MapSitkoCore();
return app.MapRazorComponents<TRootComponent>()
.AddInteractiveServerRenderMode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Diagnostics;
using System.Security.Claims;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.Logging;

namespace Sitko.Core.Blazor.Server;

public abstract class
BaseRevalidatingServerAuthenticationStateProvider<TUserInfo> : RevalidatingServerAuthenticationStateProvider
{
private readonly PersistentComponentState state;

private readonly PersistingComponentStateSubscription subscription;

private Task<AuthenticationState>? authenticationStateTask;

protected BaseRevalidatingServerAuthenticationStateProvider(ILoggerFactory loggerFactory,
PersistentComponentState persistentComponentState) : base(loggerFactory)
{
state = persistentComponentState;

AuthenticationStateChanged += OnAuthenticationStateChanged;
subscription = state.RegisterOnPersisting(OnPersistingAsync, RenderMode.InteractiveWebAssembly);
}

protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30);

protected override Task<bool> ValidateAuthenticationStateAsync(
AuthenticationState authenticationState, CancellationToken cancellationToken) =>
Task.FromResult(true);

private void OnAuthenticationStateChanged(Task<AuthenticationState> task) => authenticationStateTask = task;

private async Task OnPersistingAsync()
{
if (authenticationStateTask is null)
{
throw new UnreachableException($"Authentication state not set in {nameof(OnPersistingAsync)}().");
}

var authenticationState = await authenticationStateTask;
var principal = authenticationState.User;

if (principal.Identity?.IsAuthenticated == true)
{
var userInfo = GetUserInfo(principal);
if (userInfo is not null)
{
state.PersistAsJson(typeof(TUserInfo).FullName!, userInfo);
}
}
}

protected abstract TUserInfo? GetUserInfo(ClaimsPrincipal principal);

protected override void Dispose(bool disposing)
{
subscription.Dispose();
AuthenticationStateChanged -= OnAuthenticationStateChanged;
base.Dispose(disposing);
}
}
3 changes: 3 additions & 0 deletions src/Sitko.Core.Blazor.Server/Sitko.Core.Blazor.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<ProjectReference Include="..\Sitko.Core.App.Web\Sitko.Core.App.Web.csproj"/>
<ProjectReference Include="..\Sitko.Core.Blazor\Sitko.Core.Blazor.csproj"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;

namespace Sitko.Core.Blazor.Wasm;

public abstract class BasePersistentAuthenticationStateProvider<TUser>: AuthenticationStateProvider
{
// ReSharper disable once StaticMemberInGenericType
private static readonly Task<AuthenticationState> DefaultUnauthenticatedTask =
Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));

private readonly Task<AuthenticationState> authenticationStateTask = DefaultUnauthenticatedTask;

protected BasePersistentAuthenticationStateProvider(PersistentComponentState state)
{
if (!state.TryTakeFromJson<TUser>(nameof(TUser), out var userInfo) || userInfo is null)
{
return;
}

// ReSharper disable once VirtualMemberCallInConstructor
var claims = GetClaims(userInfo);

authenticationStateTask = Task.FromResult(
new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(claims,
authenticationType: nameof(BasePersistentAuthenticationStateProvider<TUser>)))));
}

protected abstract Claim[] GetClaims(TUser user);

public override Task<AuthenticationState> GetAuthenticationStateAsync() => authenticationStateTask;
}
2 changes: 2 additions & 0 deletions src/Sitko.Core.Blazor.Wasm/Sitko.Core.Blazor.Wasm.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication"/>
<PackageReference Include="Microsoft.Extensions.Localization" />
<PackageReference Include="Serilog.Sinks.BrowserConsole"/>
</ItemGroup>
<ItemGroup>
Expand Down
35 changes: 35 additions & 0 deletions src/Sitko.Core.Blazor.Wasm/WebAssemblyHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
Expand Down Expand Up @@ -59,4 +60,38 @@ public static ISitkoCoreBlazorApplicationBuilder AddSitkoCoreBlazorWasm(this Web
string[] args) =>
ApplicationBuilderFactory.GetOrCreateApplicationBuilder(builder,
applicationBuilder => new SitkoCoreBlazorWasmApplicationBuilder(applicationBuilder, args));

public static async Task RunApplicationAsync(this WebAssemblyHostBuilder builder)
{
var host = builder.Build();
var lifecycle = host.Services.GetRequiredService<IApplicationLifecycle>();
await lifecycle.StartingAsync(CancellationToken.None);
await lifecycle.StartedAsync(CancellationToken.None);
await host.RunAsync();
await lifecycle.StoppingAsync(CancellationToken.None);
await lifecycle.StoppedAsync(CancellationToken.None);
}

public static WebAssemblyHostBuilder ConfigureLocalization(this WebAssemblyHostBuilder builder, string culture)
{
builder.Services.AddLocalization();
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("ru-RU");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("ru-RU");


return builder;
}

public static ISitkoCoreBlazorWasmApplicationBuilder AddWebAssemblyAuth<TAuthenticationStateProvider>(
this ISitkoCoreBlazorWasmApplicationBuilder builder)
where TAuthenticationStateProvider : AuthenticationStateProvider
{
builder.ConfigureServices(services =>
{
services.AddAuthorizationCore();
services.AddCascadingAuthenticationState();
services.AddSingleton<AuthenticationStateProvider, TAuthenticationStateProvider>();
});
return builder;
}
}

0 comments on commit a197582

Please sign in to comment.