Search Results for

    Show / Hide Table of Contents

    Class CustomAuthStateProvider

    Custom authentication state provider for Blazor WebAssembly applications.

    Inheritance
    object
    AuthenticationStateProvider
    CustomAuthStateProvider
    Inherited Members
    AuthenticationStateProvider.NotifyAuthenticationStateChanged(Task<AuthenticationState>)
    AuthenticationStateProvider.AuthenticationStateChanged
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    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
    AuthenticationStateProvider.GetAuthenticationStateAsync()
    Remarks

    This method implements a multi-layer caching strategy:

    1. First checks the in-memory cache for performance
    2. Falls back to browser localStorage for persistence
    3. Extracts and validates claims from the JWT token
    4. 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:

    1. Stores tokens in the in-memory cache for immediate use
    2. Persists tokens in browser localStorage for session restoration
    3. Updates the authentication state to reflect the new logged-in state
    In this article
    Back to top Generated by DocFX