feat(auth): synchronize SSR cookie authentication with WASM client and support returnUrl navigation
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
@using Microsoft.AspNetCore.Components.Authorization
|
||||
@inject PersistentComponentState ApplicationState
|
||||
@inject AuthenticationStateProvider AuthenticationStateProvider
|
||||
@implements IDisposable
|
||||
|
||||
@code {
|
||||
private PersistingComponentStateSubscription _subscription;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
_subscription = ApplicationState.RegisterOnPersisting(PersistAuthenticationStateAsync);
|
||||
}
|
||||
|
||||
private async Task PersistAuthenticationStateAsync()
|
||||
{
|
||||
var authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
||||
var principal = authenticationState.User;
|
||||
|
||||
if (principal.Identity?.IsAuthenticated == true)
|
||||
{
|
||||
var email = principal.FindFirst(System.Security.Claims.ClaimTypes.Email)?.Value ?? principal.Identity.Name;
|
||||
var tenantId = principal.FindFirst("TenantId")?.Value ?? "global";
|
||||
var roles = string.Join(",", principal.FindAll(System.Security.Claims.ClaimTypes.Role).Select(c => c.Value));
|
||||
|
||||
if (email != null)
|
||||
{
|
||||
ApplicationState.PersistAsJson("UserInfo", new UserInfo
|
||||
{
|
||||
Email = email,
|
||||
TenantId = tenantId,
|
||||
Roles = roles
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_subscription.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user