Skip to content

Commit 883c7fc

Browse files
authored
Invalidate Cached WASM Auth Token on Authentication State Change (#36358)
* Invalidate Cached WASM Auth Token on Authentication State Change * PR Feedback * Remove Public API
1 parent f17b845 commit 883c7fc

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class RemoteAuthenticatorViewCore<TAuthenticationState> : ComponentBase w
8585
[Parameter] public EventCallback<TAuthenticationState> OnLogOutSucceeded { get; set; }
8686

8787
/// <summary>
88-
/// Gets or sets the <see cref="IJSRuntime"/> to use for performin JavaScript interop.
88+
/// Gets or sets the <see cref="IJSRuntime"/> to use for performing JavaScript interop.
8989
/// </summary>
9090
[Inject] internal IJSRuntime JS { get; set; }
9191

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
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;
5-
using System.Collections.Generic;
64
using System.Linq;
75
using System.Net.Http;
86
using System.Net.Http.Headers;
9-
using System.Threading;
10-
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Components.Authorization;
118

129
namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
1310
{
@@ -16,10 +13,11 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
1613
/// Access tokens will only be added when the request URI is within one of the base addresses configured using
1714
/// <see cref="ConfigureHandler(IEnumerable{string}, IEnumerable{string}, string)"/>.
1815
/// </summary>
19-
public class AuthorizationMessageHandler : DelegatingHandler
16+
public class AuthorizationMessageHandler : DelegatingHandler, IDisposable
2017
{
2118
private readonly IAccessTokenProvider _provider;
2219
private readonly NavigationManager _navigation;
20+
private readonly AuthenticationStateChangedHandler _authenticationStateChangedHandler;
2321
private AccessToken _lastToken;
2422
private AuthenticationHeaderValue _cachedHeader;
2523
private Uri[] _authorizedUris;
@@ -36,6 +34,13 @@ public AuthorizationMessageHandler(
3634
{
3735
_provider = provider;
3836
_navigation = navigation;
37+
38+
// Invalidate the cached _lastToken when the authentication state changes
39+
if (_provider is AuthenticationStateProvider authStateProvider)
40+
{
41+
_authenticationStateChangedHandler = _ => { _lastToken = null; };
42+
authStateProvider.AuthenticationStateChanged += _authenticationStateChangedHandler;
43+
}
3944
}
4045

4146
/// <inheritdoc />
@@ -120,5 +125,15 @@ public AuthorizationMessageHandler ConfigureHandler(
120125

121126
return this;
122127
}
128+
129+
130+
void IDisposable.Dispose()
131+
{
132+
if (_provider is AuthenticationStateProvider authStateProvider)
133+
{
134+
authStateProvider.AuthenticationStateChanged -= _authenticationStateChangedHandler;
135+
}
136+
Dispose(disposing: true);
137+
}
123138
}
124139
}

src/Components/WebAssembly/testassets/Wasm.Authentication.Server/Properties/launchSettings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"Wasm.Authentication.Server": {
1212
"commandName": "Project",
1313
"launchBrowser": true,
14+
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
1415
"environmentVariables": {
1516
"ASPNETCORE_ENVIRONMENT": "Development"
1617
},
@@ -19,6 +20,7 @@
1920
"IIS Express": {
2021
"commandName": "IISExpress",
2122
"launchBrowser": true,
23+
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
2224
"environmentVariables": {
2325
"ASPNETCORE_ENVIRONMENT": "Development"
2426
}

0 commit comments

Comments
 (0)