Skip to content

Commit 0a88fce

Browse files
Copilotilonatommy
andcommitted
Update DeserializedAuthenticationStateProvider to use declarative persistent state
Co-authored-by: ilonatommy <[email protected]>
1 parent 6c2de75 commit 0a88fce

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

src/Components/WebAssembly/WebAssembly.Authentication/src/Services/DeserializedAuthenticationStateProvider.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication;
1111

1212
internal sealed class DeserializedAuthenticationStateProvider : AuthenticationStateProvider
1313
{
14-
// Do not change. This must match all versions of the server-side AuthenticationStateSerializer.PersistenceKey.
15-
private const string PersistenceKey = $"__internal__{nameof(AuthenticationState)}";
16-
1714
private static readonly Task<AuthenticationState> _defaultUnauthenticatedTask =
1815
Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
1916

20-
private Task<AuthenticationState> _authenticationStateTask = _defaultUnauthenticatedTask;
17+
private readonly Task<AuthenticationState> _authenticationStateTask;
2118

2219
[SupplyParameterFromPersistentComponentState]
2320
private AuthenticationStateData? AuthStateData { get; set; }
@@ -29,20 +26,11 @@ internal sealed class DeserializedAuthenticationStateProvider : AuthenticationSt
2926
[DynamicDependency(JsonSerialized, typeof(AuthenticationStateData))]
3027
[DynamicDependency(JsonSerialized, typeof(IList<ClaimData>))]
3128
[DynamicDependency(JsonSerialized, typeof(ClaimData))]
32-
public DeserializedAuthenticationStateProvider(PersistentComponentState state, IOptions<AuthenticationStateDeserializationOptions> options)
29+
public DeserializedAuthenticationStateProvider(IOptions<AuthenticationStateDeserializationOptions> options)
3330
{
34-
// For backwards compatibility, try to read from the direct state first
35-
if (AuthStateData is null &&
36-
state.TryTakeFromJson<AuthenticationStateData?>(PersistenceKey, out var authenticationStateData) &&
37-
authenticationStateData is not null)
38-
{
39-
AuthStateData = authenticationStateData;
40-
}
41-
42-
if (AuthStateData is not null)
43-
{
44-
_authenticationStateTask = options.Value.DeserializationCallback(AuthStateData);
45-
}
31+
_authenticationStateTask = AuthStateData is not null
32+
? options.Value.DeserializationCallback(AuthStateData)
33+
: _defaultUnauthenticatedTask;
4634
}
4735

4836
public override Task<AuthenticationState> GetAuthenticationStateAsync() => _authenticationStateTask;

0 commit comments

Comments
 (0)