Class JwtAuthStateProvider
Authentication state provider that uses JWT tokens for authentication.
Inherited Members
Namespace: OEMS.Core.Application
Assembly: OEMS.Core.dll
Syntax
public class JwtAuthStateProvider : AuthenticationStateProvider
Remarks
This provider manages JWT token handling for Blazor applications, including:
- Token validation and storage in browser local storage
- User authentication state management
- Refresh token integration
- User authentication notifications
Constructors
JwtAuthStateProvider(ILocalStorageService, IAuthService)
Initializes a new instance of the JWT authentication state provider.
Declaration
public JwtAuthStateProvider(ILocalStorageService localStorage, IAuthService authService)
Parameters
Type | Name | Description |
---|---|---|
ILocalStorageService | localStorage | Local storage service for storing tokens |
IAuthService | authService | Authentication service for validating tokens |
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 retrieves the JWT token from local storage, validates it, and extracts the claims to determine the user's authentication state.
LogoutAsync()
Logs the user out by removing authentication tokens from local storage.
Declaration
public Task LogoutAsync()
Returns
Type | Description |
---|---|
Task | Task representing the asynchronous logout operation |
NotifyUserAuthentication(string)
Notifies the framework that the authentication state has changed to authenticated.
Declaration
public void NotifyUserAuthentication(string token)
Parameters
Type | Name | Description |
---|---|---|
string | token | JWT token containing the user's claims |
NotifyUserLogout()
Notifies the framework that the authentication state has changed to unauthenticated.
Declaration
public void NotifyUserLogout()
SetTokenAsync(string, string, DateTime?)
Stores authentication tokens in local storage and updates the authentication state.
Declaration
public Task<bool> SetTokenAsync(string token, string refreshToken = null, DateTime? expiryDate = null)
Parameters
Type | Name | Description |
---|---|---|
string | token | JWT token for authenticating the user |
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 |