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

.NET MAUI Android应用WebAuthenticator调用API挂起求助

解决.NET MAUI Android中WebAuthenticator挂起的问题

核心原因:同步阻塞UI线程导致死锁

你代码中使用WebAuthenticator.Default.AuthenticateAsync(...).Result的方式会阻塞UI线程,而AuthenticateAsync内部需要依赖UI线程完成后续回调操作,这种同步等待的方式会造成死锁,导致整个操作挂起无响应。

修复方案:改用异步等待(await)

将事件处理方法改为异步方法,使用await替代.Result,避免阻塞UI线程:

private async void OnFacebookClicked(object sender, EventArgs e)
{
    try
    {
        WebAuthenticatorResult authResult = await WebAuthenticator.Default.AuthenticateAsync(
            new Uri("https://testwebsite.azurewebsites.net/mobileauth/Facebook"),
            new Uri("xamarinessentials://"));

        string accessToken = authResult?.AccessToken;
        // 后续处理逻辑
    }
    catch (TaskCanceledException)
    {
        // 用户取消认证
    }
    catch (Exception ex)
    {
        // 处理其他异常
    }
}

额外检查项

  • Android Manifest配置:确保在Android项目的AndroidManifest.xml中正确配置自定义scheme的intent-filter,示例如下:
    <activity android:name="microsoft.maui.controls.platform.android.MauiAppCompatActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="xamarinessentials" />
        </intent-filter>
    </activity>
    
  • 后端回调地址:确认后端的认证回调逻辑正确跳转到xamarinessentials://这个自定义scheme,无拼写错误或跳转失败情况。
  • 权限配置:确保Android项目中添加了android.permission.INTERNET网络权限,若使用Chrome自定义标签页,需确认相关配置正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 11:40:13