1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- using System ;
5
- using System . Collections . Generic ;
6
4
using System . Linq ;
7
5
using System . Net . Http ;
8
6
using System . Net . Http . Headers ;
9
- using System . Threading ;
10
- using System . Threading . Tasks ;
7
+ using Microsoft . AspNetCore . Components . Authorization ;
11
8
12
9
namespace Microsoft . AspNetCore . Components . WebAssembly . Authentication
13
10
{
@@ -16,10 +13,11 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
16
13
/// Access tokens will only be added when the request URI is within one of the base addresses configured using
17
14
/// <see cref="ConfigureHandler(IEnumerable{string}, IEnumerable{string}, string)"/>.
18
15
/// </summary>
19
- public class AuthorizationMessageHandler : DelegatingHandler
16
+ public class AuthorizationMessageHandler : DelegatingHandler , IDisposable
20
17
{
21
18
private readonly IAccessTokenProvider _provider ;
22
19
private readonly NavigationManager _navigation ;
20
+ private readonly AuthenticationStateChangedHandler _authenticationStateChangedHandler ;
23
21
private AccessToken _lastToken ;
24
22
private AuthenticationHeaderValue _cachedHeader ;
25
23
private Uri [ ] _authorizedUris ;
@@ -36,6 +34,13 @@ public AuthorizationMessageHandler(
36
34
{
37
35
_provider = provider ;
38
36
_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
+ }
39
44
}
40
45
41
46
/// <inheritdoc />
@@ -120,5 +125,15 @@ public AuthorizationMessageHandler ConfigureHandler(
120
125
121
126
return this ;
122
127
}
128
+
129
+
130
+ void IDisposable . Dispose ( )
131
+ {
132
+ if ( _provider is AuthenticationStateProvider authStateProvider )
133
+ {
134
+ authStateProvider . AuthenticationStateChanged -= _authenticationStateChangedHandler ;
135
+ }
136
+ Dispose ( disposing : true ) ;
137
+ }
123
138
}
124
139
}
0 commit comments