.NET Blazor MAUI UAE Pass认证完成后无法重定向回移动应用
MAUI Blazor集成UAE Pass认证后无法重定向回Android应用的解决方案
问题背景
使用MAUI Blazor(C#)开发集成UAE Pass认证的应用,已成功获取UAE Pass授权码,但认证完成后无法重定向回移动应用。重定向URL示例:https://patientmobile/?code=c8***8eb-c**9-35b9-XXX-c5ab***b629&state=XXX
已配置的内容:
- MainActivity.cs中的Intent Filter:
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.ActionView, Intent.CategoryDefault, Intent.CategoryBrowsable, }, DataScheme = "https", DataHost = "patientmobile", DataPathPrefix = "/")]
- Android清单文件配置:
<activity android:name=".MainActivity" 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="https" android:host="patientmobile" android:pathPrefix="/" /> </intent-filter> </activity>
问题根源
patientmobile作为host搭配https scheme时,Android系统会默认将其视为真实域名,优先尝试通过浏览器解析,而非唤起你的应用。只有当系统确认该域名与你的应用存在绑定关系时,才会触发应用跳转。
解决方案
方案1:使用自定义Scheme(推荐,无需域名)
这是移动端跳转最常用的方式,无需依赖真实域名:
- 修改Intent Filter和清单文件的scheme为自定义标识(比如
patientmobile),同时调整host:- MainActivity.cs:
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable, }, DataScheme = "patientmobile", DataHost = "auth", DataPathPrefix = "/")] - Android清单文件:
<activity android:name=".MainActivity" 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="patientmobile" android:host="auth" android:pathPrefix="/" /> </intent-filter> </activity>
- MainActivity.cs:
- 登录UAE Pass后台,将重定向URL修改为:
patientmobile://auth/?code=xxx&state=xxx
方案2:继续使用HTTPS Scheme(需真实域名支持)
若必须使用https scheme,需完成域名与应用的绑定:
- 确保你拥有
patientmobile真实域名,并配置好HTTPS证书。 - 在域名根目录的
.well-known文件夹下放置assetlinks.json文件,内容示例(替换为你的应用信息):[{ "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "com.yourcompany.yourapp", "sha256_cert_fingerprints": ["AA:BB:CC:...你的应用SHA256签名指纹"] } }] - 确保应用的包名、签名指纹与
assetlinks.json中的信息完全一致,且文件可正常访问。
额外必要配置
在MAUI Blazor中,需要接收Intent传递的重定向参数:
在MainActivity.cs中重写OnNewIntent方法,获取重定向URL并传递给Blazor页面处理:
protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); if (intent.Data != null) { var redirectUrl = intent.Data.ToString(); // 将URL传递给Blazor页面,可通过静态变量、MAUI消息中心等方式实现 App.RedirectAuthUrl = redirectUrl; } }
随后在Blazor页面中读取该URL,解析授权码和state参数完成后续认证流程。
内容的提问来源于stack exchange,提问作者Edmatix Mazik
相关产品推荐
相关产品推荐

