MAUI迁移后无法通过Deep Link启动浏览器完成Dropbox认证
MAUI .NET8 Android端Dropbox认证Activity无法启动问题
正在将Xamarin Forms应用迁移至MAUI(.NET8),Android端移植了原Xamarin中可用的Dropbox认证代码到MAUI服务,但认证流程停滞。使用Dropbox.Api v6.37.0(与Xamarin版本一致),Xamarin中可正常启动Dropbox认证浏览器页面,但MAUI中Activity无法启动。Dropbox API生成的URL格式正确,无需怀疑API问题。
相关代码
RedirectHandler Activity代码
[Activity(Label = "Dropbox", Theme = "@style/MainTheme", MainLauncher = false, Exported = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Nosensor)] [IntentFilter ( new [] { Intent.ActionView }, Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault }, DataScheme = "au.com.technologyinsights.myapp" )] public class RedirectHandler : Activity { protected async override void OnCreate ( Bundle savedInstanceState ) { base.OnCreate( savedInstanceState ); // *** MAUI中从未执行到此处 *** Intent intent = Intent; // 启动此Activity的Intent if ( Intent.Action == Intent.ActionView ) { var uri = intent.Data; ... ... } }
DropboxAuthService代码
internal partial class DropboxAuthService { Intent intent; internal partial bool IsBrowserInstalled () // *** MAUI中返回true *** // 检查是否安装了浏览器 { string url = "https://google.com"; // 任意URL均可 Android.Net.Uri webAddress = Android.Net.Uri.Parse ( url ); Intent intentWeb = new Intent ( Intent.ActionView, webAddress ); Context currentContext = Platform.AppContext; Android.Content.PM.PackageManager packageManager = currentContext.PackageManager; ComponentName componentName = intentWeb.ResolveActivity ( packageManager ); return componentName != null; } internal partial void OpenBrowser ( string url ) // 打开默认浏览器 { intent = new Intent ( Intent.ActionView, Android.Net.Uri.Parse ( url ) ); Context currentContext = Platform.AppContext; currentContext.StartActivity ( intent ); // *** MAUI中执行到此行,但无后续反应,Activity未触发 *** } }
认证流程调用代码
// 以下5行使用Dropbox API生成认证URI - MAUI中生成的URI格式正确 DropboxCertHelper.InitializeCertPinning (); pkce = new PKCEOAuthFlow (); // 生成PKCE所需的code verifier和code challenge StatePKCE = Guid.NewGuid ().ToString ( "N" ); Uri authorizeUri = pkce.GetAuthorizeUri ( OAuthResponseType.Code, clientId: clientId, redirectUri:redirectUri, state: StatePKCE, tokenAccessType: TokenAccessType.Offline, scopeList: null, includeGrantedScopes: IncludeGrantedScopes.None ); // 注:authorizeUri格式示例: // https://www.dropbox.com/oauth2/authorize?response_type=code&client_id=xxxxx&redirect_uri=com.mydomain.myapp%3A%2Foauth2redirect&state=51cbbd2b7bce4d7990bc72fc95991375&token_access_type=offline&code_challenge_method=S256&code_challenge=yyyyy // 在浏览器中打开认证URL dropboxAuthService.OpenBrowser ( authorizeUri.AbsoluteUri ); // *** MAUI中执行到此行 *** // 注:MAUI中生成的实际URL格式正确,示例: // https://www.dropbox.com/oauth2/authorize?response_type=code&client_id=XXXX&redirect_uri=au.com.technologyinsights.behayve%3A%2Foauth2redirect&state=612f9abbd8ed4beda2212d730583cef0&token_access_type=offline&code_challenge_method=S256&code_challenge=s9ylxHmnpIbJR46t1N7lxHZNT7yP5y9wyM4Lu9LbcIs
已排查情况
- Activity的DataScheme与AndroidManifest中的包名匹配
- Dropbox端配置正确(原Xamarin应用可正常运行)
- 升级至Dropbox.Api v7.0.0后问题依旧
IsBrowserInstalled方法返回true,确认设备已安装浏览器OpenBrowser方法执行到StartActivity,但无后续反应,RedirectHandler的OnCreate从未触发
请求建议
恳请提供修复或进一步排查的方向建议。
内容的提问来源于stack exchange,提问作者BillF
相关产品推荐
相关产品推荐

