You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI Blazor应用中如何使生物识别认证在运行或聚焦时持续有效?

Blazor MAUI生物识别认证状态持久化解决方案

1. 内存单例维护运行时认证状态

创建全局单例服务存储认证状态,确保应用运行期间状态一致:

public class AuthStateService
{
    public bool IsAuthenticated { get; private set; }

    public void SetAuthenticated(bool isAuthenticated)
    {
        IsAuthenticated = isAuthenticated;
    }

    public void ClearAuthentication()
    {
        IsAuthenticated = false;
    }
}

在MauiProgram.cs中注册为单例:

builder.Services.AddSingleton<AuthStateService>();

生物识别成功后更新状态:

// 生物识别认证通过后
authStateService.SetAuthenticated(true);

需要验证的Blazor组件中注入服务判断:

@inject AuthStateService AuthState
@inject NavigationManager NavManager

@code {
    protected override void OnInitialized()
    {
        if (!AuthState.IsAuthenticated)
        {
            NavManager.NavigateTo("/auth");
        }
    }
}

2. 结合SecureStorage持久化Token(修复Token方案)

如果需要跨应用重启保留状态,用MAUI内置的SecureStorage存储认证Token,同步内存状态:

修改AuthStateService:

public class AuthStateService
{
    private readonly ISecureStorage _secureStorage;
    public bool IsAuthenticated { get; private set; }

    public AuthStateService(ISecureStorage secureStorage)
    {
        _secureStorage = secureStorage;
        // 初始化时从SecureStorage加载状态
        _ = LoadAuthStateAsync();
    }

    private async Task LoadAuthStateAsync()
    {
        var token = await _secureStorage.GetAsync("AuthToken");
        IsAuthenticated = !string.IsNullOrEmpty(token);
    }

    public async Task SetAuthenticatedAsync()
    {
        // 生成随机Token
        var token = Guid.NewGuid().ToString();
        await _secureStorage.SetAsync("AuthToken", token);
        IsAuthenticated = true;
    }

    public async Task ClearAuthenticationAsync()
    {
        await _secureStorage.RemoveAsync("AuthToken");
        IsAuthenticated = false;
    }
}

注册时直接注入ISecureStorage(MAUI已默认注册):

builder.Services.AddSingleton<AuthStateService>();

生物识别成功后调用:

await authStateService.SetAuthenticatedAsync();

3. 路由拦截实现全局认证校验

在共享布局组件中添加全局校验,避免未认证访问受限页面:

@inherits LayoutComponentBase
@inject AuthStateService AuthState
@inject NavigationManager NavManager

@code {
    protected override async Task OnInitializedAsync()
    {
        // 排除认证页面路由
        var currentPath = NavManager.Uri.Replace(NavManager.BaseUri, "/");
        if (!currentPath.StartsWith("/auth") && !AuthState.IsAuthenticated)
        {
            NavManager.NavigateTo("/auth");
        }
    }
}

4. 应用激活时重新验证(增强安全性)

订阅MAUI应用激活事件,在应用从后台切回时重新校验认证状态,必要时重新触发生物识别:

在MauiProgram.cs中添加:

var app = builder.Build();

Application.Current.Activated += async (s, e) =>
{
    var authState = app.Services.GetRequiredService<AuthStateService>();
    // 可选:检查Token有效期或直接要求重新认证
    if (authState.IsAuthenticated)
    {
        var result = await Fingerprint.Current.AuthenticateAsync(new AuthenticationRequestConfiguration("重新验证身份", "请确认是您本人操作"));
        if (!result.Authenticated)
        {
            await authState.ClearAuthenticationAsync();
            var navManager = app.Services.GetRequiredService<NavigationManager>();
            navManager.NavigateTo("/auth");
        }
    }
};

return app;

内容的提问来源于stack exchange,提问作者FedericoLovera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 21:57:22