Class CustomAuthStateProvider
Custom authentication state provider for Blazor WebAssembly applications.
Inherited Members
Namespace: OEMS.UI.Auth
Assembly: OEMS.UI.dll
Syntax
public class CustomAuthStateProvider : AuthenticationStateProvider
Remarks
This provider handles authentication in the client-side context, using:
- In-memory cache for performance during page navigation
- Browser local storage for persistence across sessions
- JWT token validation and claim extraction
Constructors
CustomAuthStateProvider(IJSRuntime, AuthenticationStateCache)
Initializes a new instance of the custom authentication state provider.
Declaration
public CustomAuthStateProvider(IJSRuntime jsRuntime, AuthenticationStateCache authCache)
Parameters
Type | Name | Description |
---|---|---|
IJSRuntime | jsRuntime | JavaScript runtime for local storage access |
AuthenticationStateCache | authCache | Cache for storing authentication state |
Methods
GetAuthenticationStateAsync()
Gets the current authentication state of the user.
Declaration
public override Task<AuthenticationState> GetAuthenticationStateAsync()
Returns
Type | Description |
---|---|
Task<AuthenticationState> | Authentication state containing the user's claims if authenticated, or an empty claims identity if not authenticated. |
Overrides
Remarks
This method implements a multi-layer caching strategy:
- First checks the in-memory cache for performance
- Falls back to browser localStorage for persistence
- Extracts and validates claims from the JWT token
- Updates the cache with the current state
LogoutAsync()
Logs the user out by removing all authentication tokens and notifying observers.
Declaration
public Task LogoutAsync()
Returns
Type | Description |
---|---|
Task | Task representing the asynchronous logout operation |
NotifyUserAuthentication(string)
Notifies the authentication system that a user has been authenticated.
Declaration
public void NotifyUserAuthentication(string token)
Parameters
Type | Name | Description |
---|---|---|
string | token | JWT token containing the user's claims |
Remarks
This method extracts claims from the JWT token, creates a new authenticated identity, and notifies all authentication state observers of the change.
NotifyUserLogout()
Notifies the authentication system that a user has been logged out.
Declaration
public void NotifyUserLogout()
Remarks
Creates an unauthenticated identity and notifies all authentication state observers of the change to logged-out state.
SetTokenAsync(string, string, DateTime?)
Stores authentication tokens and notifies the system of user authentication.
Declaration
public Task<bool> SetTokenAsync(string token, string refreshToken = null, DateTime? expiryDate = null)
Parameters
Type | Name | Description |
---|---|---|
string | token | JWT token for authentication |
string | refreshToken | Optional refresh token for obtaining new JWT tokens |
DateTime? | expiryDate | Optional expiration date of the JWT token |
Returns
Type | Description |
---|---|
Task<bool> | True if tokens were successfully stored, otherwise false |
Remarks
This method implements a dual storage strategy:
- Stores tokens in the in-memory cache for immediate use
- Persists tokens in browser localStorage for session restoration
- Updates the authentication state to reflect the new logged-in state