Skip to content

[Blazor] Update AuthenticationStateProvider to use declarative persistent component state #62063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication;

internal sealed class DeserializedAuthenticationStateProvider : AuthenticationStateProvider
{
// Do not change. This must match all versions of the server-side AuthenticationStateSerializer.PersistenceKey.
private const string PersistenceKey = $"__internal__{nameof(AuthenticationState)}";

private static readonly Task<AuthenticationState> _defaultUnauthenticatedTask =
Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));

private readonly Task<AuthenticationState> _authenticationStateTask = _defaultUnauthenticatedTask;
private readonly Task<AuthenticationState> _authenticationStateTask;

[SupplyParameterFromPersistentComponentState]
private AuthenticationStateData? AuthStateData { get; set; }

[UnconditionalSuppressMessage(
"Trimming",
Expand All @@ -26,14 +26,11 @@ internal sealed class DeserializedAuthenticationStateProvider : AuthenticationSt
[DynamicDependency(JsonSerialized, typeof(AuthenticationStateData))]
[DynamicDependency(JsonSerialized, typeof(IList<ClaimData>))]
[DynamicDependency(JsonSerialized, typeof(ClaimData))]
public DeserializedAuthenticationStateProvider(PersistentComponentState state, IOptions<AuthenticationStateDeserializationOptions> options)
public DeserializedAuthenticationStateProvider(IOptions<AuthenticationStateDeserializationOptions> options)
{
if (!state.TryTakeFromJson<AuthenticationStateData?>(PersistenceKey, out var authenticationStateData) || authenticationStateData is null)
{
return;
}

_authenticationStateTask = options.Value.DeserializationCallback(authenticationStateData);
_authenticationStateTask = AuthStateData is not null
? options.Value.DeserializationCallback(AuthStateData)
: _defaultUnauthenticatedTask;
}

public override Task<AuthenticationState> GetAuthenticationStateAsync() => _authenticationStateTask;
Expand Down
Loading