You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
i was looking for a way to refresh access tokens in a Blazor server side apps. The code in this library fits perfectly except the dependencies to HttpContext which is not available in Blazor server side.
My idea is to remove these dependencies where possible and make it compatible with the aforementioned scenario. The AccessTokenManagementService can then be used in a RevalidatingServerAuthenticationStateProvider to refresh the access token (and the id token along the way) periodically like
public class BlazorServerAuthenticationStateProvider : RevalidatingServerAuthenticationStateProvider
{
protected override Task<bool> ValidateAuthenticationStateAsync(AuthenticationState authenticationState, CancellationToken cancellationToken)
{
...
// gets and refreshes access token based on the "expires_at" claim when necessary,
// id token is refreshed in the process as original authorize request included the open_id scope
var accessToken = _accessTokenManagementService.GetUserAccessTokenAsync(user);
I created PR #120 that includes the necessary changes.
Now one can simply add the following code to a Blazor server side app to refresh the access token right before a HTTP request is made via UserAccessTokenHandler:
Startup.cs
// add custom user token store to avoid that AddAccessTokenManagement() registers its
// own store based on HttpContext (HttpContext is not available in blazor server side)
services.AddTransient<IUserTokenStore, UserTokenStore>();
var tokenBuilder = services.AddAccessTokenManagement(options =>
{
options.User.Scheme = AzureADB2CDefaults.OpenIdScheme;
options.User.RefreshBeforeExpiration = TimeSpan.FromMinutes(60).Subtract(TimeSpan.FromSeconds(30));
});
tokenBuilder.ConfigureBackchannelHttpClient();
...
services
// add a shared HttpClient ...
.AddHttpClient("MyHttpClient", (serviceProvider, httpClient) =>
{
httpClient.BaseAddress = ...
})
// ... that is injected into the following typed clients ...
.AddTypedClient<...>()
...
// ... supported by a handler that adds the access token to requests
.AddUserAccessTokenHandler();
The text was updated successfully, but these errors were encountered:
@leastprivilege When will this be included in a new nuget package? Currently i'm working with a git submodule pointing to my fork of your repo but that's not ideal.
Hi there,
i was looking for a way to refresh access tokens in a Blazor server side apps. The code in this library fits perfectly except the dependencies to HttpContext which is not available in Blazor server side.
My idea is to remove these dependencies where possible and make it compatible with the aforementioned scenario. The
AccessTokenManagementService
can then be used in aRevalidatingServerAuthenticationStateProvider
to refresh the access token (and the id token along the way) periodically likeI created PR #120 that includes the necessary changes.
Now one can simply add the following code to a Blazor server side app to refresh the access token right before a HTTP request is made via
UserAccessTokenHandler
:Startup.cs
The text was updated successfully, but these errors were encountered: