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

Android MSAL登录出现反序列化警告,回调耗时久,求原因解析

Android MSAL对接B2C登录的警告解析与延迟问题分析

问题背景

通过官方Android MSAL库对接Azure AD B2C租户,使用以下代码完成登录:

val signInParameters = SignInParameters.builder()
    .withScopes(scopes.toList())
    .withCallback(authenticationCallback)
    .withActivity(_currentActivity!!)
    .build()

_msalClient.signIn(signInParameters)

登录流程可正常完成,但Logcat中出现多条警告,且从MSAL返回结果耗时约1秒(时间戳可见)。需明确这些警告的含义,以及是否是回调延迟的原因。

日志中的警告内容

// 仅MSAL输出的警告:
The preferred username is not returned from the IdToken.
alternative_account_id was null.
Avatar URL was null.
The preferred username is not returned from the IdToken.
The preferred username is not returned from the IdToken.
Deserialization failed. Skipping AccountRecord
Deserialization failed. Skipping Credential
Deserialization failed. Skipping Credential
Deserialization failed. Skipping Credential

完整日志时间线

// 首尾行是自定义MSAL封装类的日志:首行=MSAL页面销毁,末行=MSAL认证回调成功
// 注意首尾行间隔约1秒
12:36:21.754 MsalService              I  onActivityDestroyed
12:36:22.479 SchemaUtil               W  [2023-01-29 11:36:22 - thread_name: pool-4-thread-1, correlation_id: <removed> - Android 33] The preferred username is not returned from the IdToken.
12:36:22.479 SchemaUtil...eAccountId  W  [2023-01-29 11:36:22 - thread_name: pool-4-thread-1, correlation_id: <removed> - Android 33] alternative_account_id was null.
12:36:22.479 SchemaUtil:getAvatarUrl  W  [2023-01-29 11:36:22 - thread_name: pool-4-thread-1, correlation_id: <removed> - Android 33] Avatar URL was null.
12:36:22.480 SchemaUtil               W  [2023-01-29 11:36:22 - thread_name: pool-4-thread-1, correlation_id: <removed> - Android 33] The preferred username is not returned from the IdToken.
12:36:22.482 SchemaUtil               W  [2023-01-29 11:36:22 - thread_name: pool-4-thread-1, correlation_id: <removed> - Android 33] The preferred username is not returned from the IdToken.
12:36:22.483 SharedPref...ntialCache  W  [2023-01-29 11:36:22 - thread_name: pool-4-thread-1, correlation_id: <removed> - Android 33] Deserialization failed. Skipping AccountRecord
12:36:22.816 SharedPref...ntialCache  W  [2023-01-29 11:36:22 - thread_name: pool-4-thread-1, correlation_id: <removed> - Android 33] Deserialization failed. Skipping Credential
12:36:22.823 SharedPref...ntialCache  W  [2023-01-29 11:36:22 - thread_name: pool-4-thread-1, correlation_id: <removed> - Android 33] Deserialization failed. Skipping Credential
12:36:22.825 SharedPref...ntialCache  W  [2023-01-29 11:36:22 - thread_name: pool-4-thread-1, correlation_id: <removed> - Android 33] Deserialization failed. Skipping Credential
12:36:22.894 MsalServic...onCallback  I  MSAL: authenticationCallback onSuccess

警告含义解析

1. 用户信息类警告

  • The preferred username is not returned from the IdToken:IdToken中未返回preferred_username字段。MSAL默认期望该字段存在以标识用户显示名,但B2C租户默认可能不返回,需手动配置声明。
  • alternative_account_id was null:跨平台用户关联标识alternative_account_id为空,B2C场景下需在用户流/自定义策略中配置对应的声明才会返回。
  • Avatar URL was null:用户头像URL字段为空,B2C默认不返回该信息,需额外配置头像相关声明。

2. 反序列化失败警告

  • Deserialization failed. Skipping AccountRecord/Credential:MSAL从SharedPreferences读取旧账户/凭证缓存时,因数据结构变更(如MSAL版本升级)或缓存损坏导致反序列化失败。MSAL会跳过无效记录,不影响当前登录,但会触发警告。

是否是回调延迟的原因?

从时间线看,警告集中在12:36:22.479-12:36:22.825,回调成功在12:36:22.894,间隔约0.4秒,加上页面销毁到警告开始的0.7秒,总耗时1秒左右:

  • 用户信息类警告是同步读取IdToken字段时触发,耗时极短,不会导致延迟。
  • 反序列化失败是后台线程处理缓存时触发,每次跳过操作开销极小,也不会直接造成1秒级延迟。

1秒延迟更可能是MSAL登录完成后处理令牌存储、缓存同步、账户信息整理等后台操作的正常耗时,或是B2C服务返回令牌后的网络/本地处理开销,与这些警告无直接关联。

解决方案

消除用户信息类警告

  1. 在Azure AD B2C的用户流(或自定义策略)中添加preferred_username、alternative_account_id等声明,确保IdToken返回对应字段。
  2. 若不需要这些字段,可直接忽略警告,不影响登录功能。

消除反序列化失败警告

  1. 清理MSAL缓存:调用msalClient.removeAllAccounts()清空旧缓存,重新登录后会生成新的有效缓存记录。
  2. 保持MSAL库版本稳定,避免频繁跨大版本升级,减少缓存结构变更概率。

内容的提问来源于stack exchange,提问作者me.at.coding

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 20:50:24