Fhi.HelseId.Blazor
1.0.0-alpha.3
See the version list below for details.
dotnet add package Fhi.HelseId.Blazor --version 1.0.0-alpha.3
NuGet\Install-Package Fhi.HelseId.Blazor -Version 1.0.0-alpha.3
<PackageReference Include="Fhi.HelseId.Blazor" Version="1.0.0-alpha.3" />
paket add Fhi.HelseId.Blazor --version 1.0.0-alpha.3
#r "nuget: Fhi.HelseId.Blazor, 1.0.0-alpha.3"
// Install Fhi.HelseId.Blazor as a Cake Addin #addin nuget:?package=Fhi.HelseId.Blazor&version=1.0.0-alpha.3&prerelease // Install Fhi.HelseId.Blazor as a Cake Tool #tool nuget:?package=Fhi.HelseId.Blazor&version=1.0.0-alpha.3&prerelease
Fhi.HelseId.Refit
This package contains code to simplify working with Blazor, Refit and HelseId.
Blazor does not currently let you interact with the HttpContext that is needed for normal HelseId funtionallity in a WebApi.
Some of the problems this code solves are
- HttpContext is not availible during most of the Blazor SPA lifetime
- The HelseId tokens are stored in the HttpContext which are not normally availible from most of the rendering tree in the Blazor code.
- The HelseId tokens are stored in the HttpContext which are not normally availible from a normal scoped or transient service when resolved with the ServiceProvider (DI).
- Refit uses HttpClientFactory, which creates transient DelegationHandlers, but they come from a singleton scope! This leads to us being unable to get the HelseId access token by the consumer of a Refit interface.
- We need to update cookies with new token data when refreshing HelseId token
This default setup will add a token handler to your Refit Interface in addition to letting you add multiple delegates if needed (f.ex. logging).
Usage
Include this code in your WebApi startup builder (remember to also call "builder.AddHelseIdWebAuthentication()" etc):
builder.AddHelseIdForBlazor()
.AddRefitClient<IMyRefitClient>()
...
app.UseHelseIdForBlazor();
You will also need to wrap your hole App.razor code with a CascadingStates-tag:
<CascadingStates>
... all your App.razor HTML ...
</CascadingStates>
If you want to add additional loggers add them before "AddRefitClient":
builder.AddHelseIdForBlazor()
.AddHandler<MyLoggingDelegationHandler>()
.AddRefitClient<IMyRefitClient>()
The code loads your configuration from IConfiguration using the section "HelseIdWebKonfigurasjon". If you want to override which section to use you can pass the correct section to AddHelseIdForBlazor:
builder.AddHelseIdForBlazor("HelseIdWebKonfigurasjon")
.AddRefitClient<IMyRefitClient>()
The default RefitSettings we are using use SystemTextJsonContentSerializer, is case insensitive and use camelCasing. If you want to override the default RefitSettings to use you can pass the settings to AddHelseIdForBlazor:
builder.AddHelseIdForBlazor(new RefitSettings())
.AddRefitClient<IMyRefitClient>();
Note that using this builder will automatically add a middleware for logging you out, as the default HelseId-way does not work well for Blazor apps. The URLs defaults to "/logout" and "/loggedout". You can configure the logout options by calling
builder.AddHelseIdForBlazor()
.ConfigureLogout(....);
Adding Correlation Id to all requests
Use "AddCorrelationId()" to add header propagation of the default FHI correlation id header.
builder.AddHelseIdForBlazor()
.AddCorrelationId()
.AddRefitClient<IMyRefitClient>();
A new correlation ID will be given to each request and response that does not contain the header when invoked.
More usage
If you would like to reuse some of the code to access the HttpContext for dependency injection you can hook into the code with custom implementations of a IScopedState:
builder.AddStateHandlers().AddScopedState<UserState>();
An example of a implementation of UserState could be something like this.
public class UserState : IScopedState
{
public string CorrelationId { get; set; }
public UserState() // you can even use the constructor for normal dependency injection here!
{
}
public async Task Populate(HttpContext httpContext)
{
var headerValue = string.Empty;
if (httpContext.Request.Headers.TryGetValue("X-Correlation-ID", out var values))
{
headerValue = values.FirstOrDefault();
}
else if (httpContext.Response.Headers.TryGetValue("X-Correlation-ID", out values))
{
headerValue = values.FirstOrDefault();
}
CorrelationId = string.IsNullOrEmpty(headerValue) ? Guid.NewGuid().ToString() : headerValue;
}
}
Changing default implementations
By default the code creates new HttpClients and delegates for each request, to be able to create correctly scoped delegates to apply the correct authentication token. This is bad if your system has a lot of users as it may lead to socket exhaustion.
You can change the default implementation by providing your own IScopedHttpClientFactory
services.AddSingleton<IScopedHttpClientFactory>(new MyOwnScopedHttpClientFactory());
If you are able to create a better implementation please consider making a pull request to change our ScopedHttpClientFactory.
Note that the ScopedHttpClientFactory creates the handler in the opposite direction than IHttpClientFactory. This is because we want to end up with applying authorization and correlation id before the user may add logging delegates, and finally having the default httpclienthandler as the innermost handler.
You can also change the default HttpClientHandler builder if you please. Note that if you do you might also want to change if the handlers should be disposed or not after HttpClients are disposed (defaults to true):
builder.AddHelseIdForBlazor()
.SetHttpClientHandlerBuilder(name => new HttpClientHandler())
.DisposeHandleres(true)
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Fhi.HelseId (>= 5.4.1)
- Microsoft.AspNetCore.Components (>= 6.0.26)
- Microsoft.AspNetCore.HeaderPropagation (>= 6.0.26)
- Refit (>= 7.0.0)
- Refit.HttpClientFactory (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
8.0.1 | 34 | 11/29/2024 |
8.0.0 | 76 | 11/19/2024 |
7.1.2 | 83 | 11/6/2024 |
7.1.1 | 75 | 11/1/2024 |
7.1.0 | 72 | 10/31/2024 |
7.0.1 | 86 | 10/23/2024 |
6.3.0 | 94 | 10/11/2024 |
6.2.1 | 104 | 10/7/2024 |
6.2.0 | 93 | 9/27/2024 |
6.2.0-beta.2 | 49 | 9/24/2024 |
6.2.0-beta.1 | 63 | 9/17/2024 |
6.1.0 | 108 | 9/10/2024 |
6.0.0 | 88 | 8/7/2024 |
1.1.3-alpha.1 | 57 | 5/6/2024 |
1.1.2 | 293 | 5/3/2024 |
1.1.1 | 95 | 5/2/2024 |
1.1.0 | 95 | 5/2/2024 |
1.0.1 | 124 | 4/18/2024 |
1.0.0-beta.2 | 126 | 1/7/2024 |
1.0.0-beta.1 | 77 | 1/4/2024 |
1.0.0-alpha.3 | 332 | 2/13/2024 |
1.0.0-alpha.2 | 86 | 2/2/2024 |
1.0.0-alpha.1 | 92 | 1/12/2024 |