DX.Blazor.Identity.Wasm
26.1.3.30
dotnet add package DX.Blazor.Identity.Wasm --version 26.1.3.30
NuGet\Install-Package DX.Blazor.Identity.Wasm -Version 26.1.3.30
<PackageReference Include="DX.Blazor.Identity.Wasm" Version="26.1.3.30" />
<PackageVersion Include="DX.Blazor.Identity.Wasm" Version="26.1.3.30" />
<PackageReference Include="DX.Blazor.Identity.Wasm" />
paket add DX.Blazor.Identity.Wasm --version 26.1.3.30
#r "nuget: DX.Blazor.Identity.Wasm, 26.1.3.30"
#:package DX.Blazor.Identity.Wasm@26.1.3.30
#addin nuget:?package=DX.Blazor.Identity.Wasm&version=26.1.3.30
#tool nuget:?package=DX.Blazor.Identity.Wasm&version=26.1.3.30
DX.Blazor.Identity.Wasm
Client-side ASP.NET Core Identity integration for Blazor WebAssembly apps — token storage in localStorage, an AuthenticationStateProvider that parses JWT claims, and an HTTP interceptor that silently refreshes an about-to-expire token before it's attached to outgoing requests.
Target frameworks: net9.0, net10.0
DevExpress dependency: transitive, via DX.Blazor.Identity → DX.Data.Xpo.Identity (26.1.*)
Install
dotnet add package DX.Blazor.Identity.Wasm
Requires Blazored.LocalStorage and Toolbelt.Blazor.HttpClientInterceptor (both pulled in automatically as package dependencies).
AuthenticationService (Services/AuthenticationService.cs)
public class AuthenticationService<TRegistrationModel, TAuthenticationModel> : IAuthService<TRegistrationModel, TAuthenticationModel>
{
public AuthenticationService(HttpClient client, AuthenticationStateProvider authStateProvider, ILocalStorageService localStorage);
public Task<RegistrationResponseModel> RegisterUser(TRegistrationModel model); // POST Accounts/Registration
public Task<AuthResponseModel> Login(TAuthenticationModel model); // POST Accounts/Login, stores token+refreshToken in localStorage
public Task<string> RefreshToken(); // POST token/refresh
public Task Logout(); // clears localStorage, notifies AuthStateProvider
}
public class AuthenticationService<TRegistrationModel> : AuthenticationService<TRegistrationModel, AuthenticationModel>, IAuthService<TRegistrationModel> { }
public class AuthenticationService : AuthenticationService<RegistrationModel>, IAuthService { }
On successful login, the JWT and refresh token are written to localStorage (keys authToken/refreshToken), the HttpClient's default Authorization header is set to bearer <token>, and AuthStateProvider.NotifyUserAuthentication(token) is called so the rest of the app immediately sees the new claims principal.
AuthStateProvider (AuthStateProvider.cs)
public class AuthStateProvider : AuthenticationStateProvider
{
public AuthStateProvider(HttpClient httpClient, ILocalStorageService localStorage);
public override Task<AuthenticationState> GetAuthenticationStateAsync(); // reads authToken from localStorage, parses JWT claims
public void NotifyUserAuthentication(string token);
public void NotifyUserLogout();
}
public static class JwtParser
{
public static IEnumerable<Claim> ParseClaimsFromJwt(string jwt); // base64url-decodes the JWT payload segment into claims
}
JwtParser.ParseClaimsFromJwt handles the JWT's base64url padding manually (Convert.FromBase64String needs ==/= padding restored) and specifically unpacks a JSON array under the ClaimTypes.Role key into one Claim per role — plain ASP.NET Core ClaimsPrincipal parsing doesn't do this for you when a JWT encodes multiple roles as a single array claim.
TokenService<TKey, TUser> (Services/TokenService.cs)
public class TokenService<TKey, TUser> : ITokenService<TKey, TUser>
where TUser : IdentityUser<TKey>, IIdentityRefreshToken, new()
{
public TokenService(IConfiguration configuration, UserManager<TUser> userManager);
public SigningCredentials GetSigningCredentials(); // reads JwtSettings:securityKey
public Task<List<Claim>> GetClaims(TUser user); // Name + one Role claim per role
public JwtSecurityToken GenerateTokenOptions(SigningCredentials creds, List<Claim> claims); // JwtSettings:validIssuer/validAudience/expiryInMinutes
public string GenerateRefreshToken(); // 32 random bytes, base64
public ClaimsPrincipal GetPrincipalFromExpiredToken(string token); // validates everything except lifetime
}
This is the server-side-callable token minting/validation logic — despite living in the .Wasm package, it's typically registered and invoked from your Web API project (the hosted-WASM backend) alongside DX.Blazor.Identity.Server, not executed in the browser itself.
RefreshTokenService / HttpInterceptorService (Services/)
public class RefreshTokenService
{
public RefreshTokenService(AuthenticationStateProvider authProvider, IAuthService authService);
public Task<string> TryRefreshToken(); // refreshes if the current token's "exp" claim is within 2 minutes of expiring
}
public class HttpInterceptorService
{
public HttpInterceptorService(HttpClientInterceptor interceptor, RefreshTokenService refreshTokenService);
public void RegisterEvent(); // hooks HttpClientInterceptor.BeforeSendAsync
public Task InterceptBeforeHttpAsync(object sender, HttpClientInterceptorEventArgs e);
public void DisposeEvent();
}
HttpInterceptorService is the piece that makes silent token refresh actually silent: it hooks into Toolbelt.Blazor.HttpClientInterceptor's BeforeSendAsync event, and for every outgoing request whose path doesn't contain token or accounts (to avoid refreshing while you're literally calling the login/refresh endpoints), it calls RefreshTokenService.TryRefreshToken() and — if a new token was minted — overwrites the request's Authorization header before it goes out. Register it once at app startup (RegisterEvent()) and your Razor components never need to think about token expiry.
Setup
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddHttpClientInterceptor();
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress + "api/") }.EnableIntercept(sp));
builder.Services.AddScoped<IAuthService<RegistrationModel, AuthenticationModel>, DX.Blazor.Identity.Wasm.Services.AuthenticationService>();
builder.Services.AddScoped<DX.Blazor.Identity.Wasm.Services.RefreshTokenService>();
builder.Services.AddScoped<AuthenticationStateProvider, DX.Blazor.Identity.Wasm.AuthStateProvider>();
builder.Services.AddScoped<DX.Blazor.Identity.Wasm.Services.HttpInterceptorService>();
See the root README for the complete Hosted WASM walkthrough, including the matching server-side controllers from DX.Blazor.Identity.Server.
Notes
- Pairs with
DX.Blazor.Identity.Server, which hosts theAccounts/Registration,Accounts/Login, andtoken/refreshAPI endpoints this package'sHttpClientcalls. SupportedPlatform Include="browser"is declared in the csproj — this package is intended for the WASM target and depends onBlazored.LocalStorage, which requires JS interop.
See the root README for the full package list and DevExpress version alignment notes.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Blazored.LocalStorage (>= 4.5.0)
- DX.Blazor.Identity (>= 26.1.3.32)
- Microsoft.AspNetCore.Components.Authorization (>= 10.0.14)
- Microsoft.AspNetCore.Components.Web (>= 10.0.14)
- System.IdentityModel.Tokens.Jwt (>= 8.23.0)
- Toolbelt.Blazor.HttpClientInterceptor (>= 10.2.0)
-
net9.0
- Blazored.LocalStorage (>= 4.5.0)
- DX.Blazor.Identity (>= 26.1.3.32)
- Microsoft.AspNetCore.Components.Authorization (>= 9.0.22)
- Microsoft.AspNetCore.Components.Web (>= 9.0.22)
- System.IdentityModel.Tokens.Jwt (>= 8.23.0)
- Toolbelt.Blazor.HttpClientInterceptor (>= 10.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
26.1.3.28: Rebuilt against DX.Blazor.Identity 26.1.3.30 to pick up the System.Security.Cryptography.Xml 8.0.4 fix (NU1903) transitively.
26.1.3.27: Added a README.md with technical documentation, now embedded in the package via PackageReadmeFile. Rebuilt against DX.Blazor.Identity 26.1.3.29.
26.1.3.26: Rebuilt against DX.Blazor.Identity 26.1.3.28 to pick up DevExpress v26.1.3 transitively.
23.2.3.23: Upgraded to .NET 8 and DX v23.2
22.1.4.20: Upgraded several packages
22.1.4.19: Upgraded several packages incl DevExpress v21.1.4
21.2.8.18: New helper package for DX.Blazor.Identity with everything for Blazor WASM to easily implement Identity with XPO on Blazor