Skip to content

Commit f763294

Browse files
Copilotilonatommy
andcommitted
Fix AuthenticationStateSerializer to use declarative persistent component state
Co-authored-by: ilonatommy <32700855+ilonatommy@users.noreply.github.com>
1 parent 75fc6a1 commit f763294

File tree

4 files changed

+11
-27
lines changed

4 files changed

+11
-27
lines changed

src/Components/WebAssembly/Server/src/AuthenticationStateSerializer.cs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,40 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.AspNetCore.Components.Authorization;
5-
using Microsoft.AspNetCore.Components.Web;
65
using Microsoft.Extensions.Options;
76

87
namespace Microsoft.AspNetCore.Components.WebAssembly.Server;
98

10-
internal sealed class AuthenticationStateSerializer : IHostEnvironmentAuthenticationStateProvider, IDisposable
9+
internal sealed class AuthenticationStateSerializer : IHostEnvironmentAuthenticationStateProvider
1110
{
1211
// Do not change. This must match all versions of the server-side DeserializedAuthenticationStateProvider.PersistenceKey.
1312
internal const string PersistenceKey = $"__internal__{nameof(AuthenticationState)}";
1413

15-
private readonly PersistentComponentState _state;
1614
private readonly Func<AuthenticationState, ValueTask<AuthenticationStateData?>> _serializeCallback;
17-
private readonly PersistingComponentStateSubscription _subscription;
1815

1916
private Task<AuthenticationState>? _authenticationStateTask;
2017
private AuthenticationStateData? _authenticationStateData;
2118

22-
[PersistentComponentStateStore]
19+
[SupplyParameterFromPersistentComponentState]
2320
public AuthenticationStateData? AuthStateData
2421
{
2522
get => _authenticationStateData;
2623
set => _authenticationStateData = value;
2724
}
2825

29-
public AuthenticationStateSerializer(PersistentComponentState persistentComponentState, IOptions<AuthenticationStateSerializationOptions> options)
26+
public AuthenticationStateSerializer(IOptions<AuthenticationStateSerializationOptions> options)
3027
{
31-
_state = persistentComponentState;
3228
_serializeCallback = options.Value.SerializationCallback;
33-
_subscription = persistentComponentState.RegisterOnPersisting(OnPersistingAsync, RenderMode.InteractiveWebAssembly);
34-
}
35-
36-
private async Task OnPersistingAsync()
37-
{
38-
if (_authenticationStateTask is null)
39-
{
40-
throw new InvalidOperationException($"{nameof(SetAuthenticationState)} must be called before the {nameof(PersistentComponentState)}.{nameof(PersistentComponentState.RegisterOnPersisting)} callback.");
41-
}
42-
43-
AuthStateData = await _serializeCallback(await _authenticationStateTask);
4429
}
4530

4631
/// <inheritdoc />
47-
public void SetAuthenticationState(Task<AuthenticationState> authenticationStateTask)
32+
public async void SetAuthenticationState(Task<AuthenticationState> authenticationStateTask)
4833
{
4934
_authenticationStateTask = authenticationStateTask ?? throw new ArgumentNullException(nameof(authenticationStateTask));
50-
}
51-
52-
public void Dispose()
53-
{
54-
_subscription.Dispose();
35+
36+
if (_authenticationStateTask is not null)
37+
{
38+
AuthStateData = await _serializeCallback(await _authenticationStateTask);
39+
}
5540
}
5641
}

src/Components/WebAssembly/Server/src/WebAssemblyRazorComponentsBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.AspNetCore.Components.Authorization;
66
using Microsoft.AspNetCore.Components.Endpoints.Infrastructure;
77
using Microsoft.AspNetCore.Components.Infrastructure;
8+
using Microsoft.AspNetCore.Components.Web;
89
using Microsoft.AspNetCore.Components.WebAssembly.Server;
910
using Microsoft.AspNetCore.Components.WebAssembly.Services;
1011
using Microsoft.Extensions.DependencyInjection.Extensions;

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Diagnostics.CodeAnalysis;
54
using System.Security.Claims;
65
using Microsoft.AspNetCore.Components.Authorization;
76
using Microsoft.Extensions.Options;
8-
using static Microsoft.AspNetCore.Internal.LinkerFlags;
97

108
namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication;
119

@@ -19,7 +17,6 @@ internal sealed class DeserializedAuthenticationStateProvider : AuthenticationSt
1917
[SupplyParameterFromPersistentComponentState]
2018
private AuthenticationStateData? AuthStateData { get; set; }
2119

22-
2320
public DeserializedAuthenticationStateProvider(IOptions<AuthenticationStateDeserializationOptions> options)
2421
{
2522
_authenticationStateTask = AuthStateData is not null

src/Components/WebAssembly/WebAssembly.Authentication/src/WebAssemblyAuthenticationServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Components;
77
using Microsoft.AspNetCore.Components.Authorization;
88
using Microsoft.AspNetCore.Components.Infrastructure;
9+
using Microsoft.AspNetCore.Components.Web;
910
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
1011
using Microsoft.AspNetCore.Components.WebAssembly.Authentication.Internal;
1112
using Microsoft.Extensions.DependencyInjection.Extensions;

0 commit comments

Comments
 (0)