首页 > 解决方案 > 在 Android 中使用 MSAL 时如何从 IAuthenticationResult 获取 B2CUser

问题描述

我在 B2C 多用户模式下将 MSAL 与 Android 一起使用。我下面的代码来自这个例子。它显示了如何从帐户列表中获取 B2CUsers 列表:

private void loadAccounts() {
    if (b2cApp == null) {
        return;
    }

    b2cApp.getAccounts(new IPublicClientApplication.LoadAccountsCallback() {
        @Override
        public void onTaskCompleted(final List<IAccount> result) {
            users = B2CUser.getB2CUsersFromAccountList(result);
            updateUI(users);
        }

        @Override
        public void onError(MsalException exception) {
            displayError(exception);
        }
    });
}

但我还需要在认证后获取特定的 B2CUser:

            AcquireTokenParameters parameters = new AcquireTokenParameters.Builder()
                    .startAuthorizationFromActivity(getActivity())
                    .fromAuthority(B2CConfiguration.getAuthorityFromPolicyName(policyListSpinner.getSelectedItem().toString()))
                    .withScopes(B2CConfiguration.getScopes())
                    .withPrompt(Prompt.LOGIN)
                    .withCallback(getAuthInteractiveCallback())
                    .build();

            b2cApp.acquireToken(parameters);

在 authenticationResult 之后给了我一个 IAccount:

        @Override
        public void onSuccess(IAuthenticationResult authenticationResult) {
            /* Successfully got a token, use it to call a protected resource - MSGraph */
            Log.d(TAG, "Successfully authenticated");

            /* display result info */
            displayResult(authenticationResult);

            /* Reload account asynchronously to get the up-to-date list. */
            loadAccounts();
        }

但我似乎无法将其转换为 B2CUser 或从中获取 ID:

iAuthenticationResult.getAccount().getId()

似乎返回附加到它的策略,我也无法获得 displayName。

标签: azure-ad-b2cmsal

解决方案


推荐阅读