Loading angepasst
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
|
||||
<body>
|
||||
<Routes />
|
||||
<persist-component-state />
|
||||
<script src="@Assets["_framework/blazor.web.js"]"></script>
|
||||
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
|
||||
<script>
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
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 timetracker.Shared;
|
||||
|
||||
namespace timetracker.Server;
|
||||
|
||||
public class PersistingServerAuthenticationStateProvider : ServerAuthenticationStateProvider, IDisposable
|
||||
{
|
||||
private readonly PersistentComponentState _state;
|
||||
private readonly PersistingComponentStateSubscription _subscription;
|
||||
|
||||
public PersistingServerAuthenticationStateProvider(PersistentComponentState state)
|
||||
{
|
||||
_state = state;
|
||||
_subscription = state.RegisterOnPersisting(PersistAuthenticationState);
|
||||
}
|
||||
|
||||
private async Task PersistAuthenticationState()
|
||||
{
|
||||
var authState = await GetAuthenticationStateAsync();
|
||||
var user = authState.User;
|
||||
|
||||
if (user.Identity?.IsAuthenticated == true)
|
||||
{
|
||||
var idClaim = user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
var name = user.Identity.Name;
|
||||
var isAdminClaim = user.FindFirst("IsTenantAdmin")?.Value;
|
||||
|
||||
if (idClaim != null && name != null)
|
||||
{
|
||||
var userInfo = new UserInfo
|
||||
{
|
||||
Id = int.Parse(idClaim),
|
||||
Username = name,
|
||||
IsTenantAdmin = isAdminClaim == "true",
|
||||
IsAuthenticated = true
|
||||
};
|
||||
_state.PersistAsJson("UserInfoState", userInfo);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var userInfo = new UserInfo
|
||||
{
|
||||
IsAuthenticated = false
|
||||
};
|
||||
_state.PersistAsJson("UserInfoState", userInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_subscription.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,10 @@ using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using MudBlazor.Services;
|
||||
using Serilog;
|
||||
using timetracker.Server;
|
||||
using timetracker.Server.Components;
|
||||
using timetracker.Data;
|
||||
using timetracker.Shared;
|
||||
@@ -68,6 +70,7 @@ builder.Services.AddAuthorization(options =>
|
||||
});
|
||||
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
builder.Services.AddScoped<AuthenticationStateProvider, PersistingServerAuthenticationStateProvider>();
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
builder.Services.AddTransient<ITenantProvider, TenantProvider>();
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user