MAUI Android集成Auth0抛出异常,iOS正常(已加回调类仍报错)
问题背景
MAUI项目中,iOS端与Auth0的认证流程正常运行,但Android端始终无法完成认证。已按要求实现WebAuthenticatorCallbackActivity子类并配置IntentFilter,仍持续抛出异常。
已实现代码
Android 专属回调Activity代码
using Android.Content; using Android.App; using Android.Content.PM; namespace abc.Mobile; [Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop, Exported = true)] [IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataScheme = CALLBACK_SCHEME)] public class WebAuthenticationCallbackActivity : Microsoft.Maui.Authentication.WebAuthenticatorCallbackActivity { const string CALLBACK_SCHEME = "com.abc"; }
Auth0 登录初始化与调用代码
var authenticationService = new Auth0Client(new() { Domain = "<YOUR_AUTH0_DOMAIN>", ClientId = Preferences.Get("ClientId", "xxx"), // 从Auth0控制台获取的Client Id RedirectUri = "com.abc://callback/", PostLogoutRedirectUri = "com.abc://callback/", Scope = "openid offline_access" }); authenticationResult = await authenticationService.LoginAsync(new { audience = xxx, // 从Auth0控制台获取的Audience prompt = "select_account" });
令牌获取与保存代码
authenticationResult = await authenticationService.LoginAsync(new { audience = Preferences.Get("Audience", "https://xxx.abc.com/api"), // 从Auth0控制台获取的Audience prompt = "<Insert prompt>" }); if ((!authenticationResult?.IsError ?? false) && !string.IsNullOrEmpty(authenticationResult?.AccessToken)) { ToggleButtonAndTextVisibility(true, false); } if (authenticationResult != null && authenticationResult.AccessToken != null && !authenticationResult.IsError) { // 将令牌保存到用户偏好存储 Preferences.Set("AccessToken", authenticationResult?.AccessToken); Preferences.Set("AccessTokenExpiration", authenticationResult?.AccessTokenExpiration.ToString("O")); Preferences.Set("IdToken", authenticationResult?.IdentityToken); Preferences.Set("RefreshToken", authenticationResult?.RefreshToken); Preferences.Set("IsAuthenticated", "true"); Preferences.Set("RefreshTokenDateTimeTicks", DateTime.Now.Ticks); hasAuthorization = true; }
报错信息
You must subclass the
WebAuthenticatorCallbackActivityand create an IntentFilter for it which matches yourcallbackUrl.
排查与解决方案
补全IntentFilter匹配规则
当前IntentFilter仅指定了DataScheme,但RedirectUri包含host(callback)和path(/),需要补充对应参数确保完全匹配:[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataScheme = CALLBACK_SCHEME, DataHost = "callback", DataPath = "/")]验证Auth0控制台配置
确认Auth0控制台的Allowed Callback URLs中已添加com.abc://callback/,注意大小写、结尾斜杠必须与代码中的RedirectUri完全一致。检查AndroidManifest配置
MAUI会根据Activity特性自动生成Manifest配置,但可手动验证Platform/Android/AndroidManifest.xml中是否包含以下节点:<activity android:name="abc.Mobile.WebAuthenticationCallbackActivity" android:launchMode="singleTop" android:noHistory="true" android:exported="true"> <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="com.abc" android:host="callback" android:path="/" /> </intent-filter> </activity>清理重建项目
清理MAUI项目的bin/obj目录,重新生成Android平台代码,避免缓存导致配置未生效。确认回调Activity位置
确保WebAuthenticationCallbackActivity类位于MAUI项目的Platform/Android目录下,而非共享项目中,否则Android平台无法正确识别该Activity。检查RedirectUri格式
确保RedirectUri的格式符合Android深层链接规范,com.abc://callback/中的scheme、host、path部分无多余字符或拼写错误。
内容的提问来源于stack exchange,提问作者Shahroz

