Skip to content

Invalidate Cached WASM Auth Token on Authentication State Change #36358

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

Merged
merged 3 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -85,7 +85,7 @@ public class RemoteAuthenticatorViewCore<TAuthenticationState> : ComponentBase w
[Parameter] public EventCallback<TAuthenticationState> OnLogOutSucceeded { get; set; }

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Authorization;

namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
{
Expand All @@ -16,10 +13,11 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
/// Access tokens will only be added when the request URI is within one of the base addresses configured using
/// <see cref="ConfigureHandler(IEnumerable{string}, IEnumerable{string}, string)"/>.
/// </summary>
public class AuthorizationMessageHandler : DelegatingHandler
public class AuthorizationMessageHandler : DelegatingHandler, IDisposable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HttpMessageHandler from which DelegatingHandler inherits already implements IDisposable so you could just override the existing protected virtual void Dispose(bool disposing) method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I'd originally done prior to e2351d3. The issue with that approach is it requires adding a new public API, which at this stage of the release is a bit tougher.

{
private readonly IAccessTokenProvider _provider;
private readonly NavigationManager _navigation;
private readonly AuthenticationStateChangedHandler _authenticationStateChangedHandler;
private AccessToken _lastToken;
private AuthenticationHeaderValue _cachedHeader;
private Uri[] _authorizedUris;
Expand All @@ -36,6 +34,13 @@ public AuthorizationMessageHandler(
{
_provider = provider;
_navigation = navigation;

// Invalidate the cached _lastToken when the authentication state changes
if (_provider is AuthenticationStateProvider authStateProvider)
{
_authenticationStateChangedHandler = _ => { _lastToken = null; };
authStateProvider.AuthenticationStateChanged += _authenticationStateChangedHandler;
}
}

/// <inheritdoc />
Expand Down Expand Up @@ -120,5 +125,15 @@ public AuthorizationMessageHandler ConfigureHandler(

return this;
}


void IDisposable.Dispose()
{
if (_provider is AuthenticationStateProvider authStateProvider)
{
authStateProvider.AuthenticationStateChanged -= _authenticationStateChangedHandler;
}
Dispose(disposing: true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaulted this to true.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"Wasm.Authentication.Server": {
"commandName": "Project",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
Expand All @@ -19,6 +20,7 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down