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

Xamarin Android 14从SSO返回时创建空白应用实例问题求助

问题:Xamarin应用在Android 14中SSO登录后创建新实例异常
  • 基于Xamarin开发的移动应用,Android 13及更早版本功能正常,Android 14中出现异常:切换Activity或从浏览器完成SSO登录返回应用时,系统未恢复现有进程,而是创建应用新实例,显示带有AndroidManifest标签的空白屏幕。
  • 已处理Android 14的BroadcastReceiver变更,排除该因素导致问题的可能。
  • 初始逻辑为启动SplashScreen后通过StartActivity(new Intent(this, typeof(MainActivity)));跳转至MainActivity,此操作会创建应用第二个实例并进入登录/SSO流程;调整为直接启动MainActivity后,不会创建新实例,且能正常打开Microsoft登录浏览器窗口,但完成Microsoft账户登录后,未像Android 13及更早版本那样返回原应用,而是再次创建新实例,显示空白页面及标题标签。

SSO登录代码

public async Task<UserToken> OnSignInAsync()
{
    UserToken currentUser = null;
    if (publicClientApplication == null)
    {
        this.publicClientApplication = PublicClientApplicationBuilder.Create(ClientID)
            .WithIosKeychainSecurityGroup(AppInfo.PackageName)
            .WithRedirectUri($"msal{ClientID}://auth")
            .Build();
    }

    var accounts = await this.publicClientApplication.GetAccountsAsync();
    try
    {
        try
        {
            var firstAccount = accounts.FirstOrDefault();
            var authResult = await this.publicClientApplication.AcquireTokenSilent(Scopes, firstAccount).ExecuteAsync();
            currentUser = await this.RefreshUserDataAsync(authResult?.AccessToken).ConfigureAwait(false);
            if (currentUser == null)
                throw new MsalUiRequiredException("0","Unable to get current user information.");
        }
        catch (MsalUiRequiredException ex)
        {
            // the user was not already connected.
            try
            {
                var authResult = await this.publicClientApplication.AcquireTokenInteractive(Scopes)
                                    .WithParentActivityOrWindow(ParentWindow)
                                    .ExecuteAsync();
                currentUser = await this.RefreshUserDataAsync(authResult?.AccessToken).ConfigureAwait(false);
            }
            catch (Exception ex2)
            {
                // Manage the exception with a logger as you need
                System.Diagnostics.Debug.WriteLine(ex2.ToString());
            }
        }

        if (currentUser != null)
        {
            currentUser.LoginModelOAuth = new LoginModelOAuth();
            currentUser.LoginModelOAuth.iv = Constants.IV;
            currentUser.LoginModelOAuth.kv = EncryptAES(currentUser.UserPrincipalName, Constants.IV);
        }
    }
    catch (Exception ex)
    {
        // Manage the exception with a logger as you need
        System.Diagnostics.Debug.WriteLine(ex.ToString());
        throw(ex);
    }

    return currentUser;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 23:47:10