Microsoft-authentication-library-for-dotnet: [Investigate] B2C Xamarin Sample getAccounts does not return an account after first sign-in

Created on 15 Oct 2020  路  16Comments  路  Source: AzureAD/microsoft-authentication-library-for-dotnet

My Code

        public async Task<UserContext> SingInWithoutInteractively()
        {
            UserContext newContext;
            try
            {
                // acquire token silent
                newContext = await AcquireToken();

            } catch
            {
                newContext = null;
            }

            return newContext;
        }

        public async Task<UserContext> SignInAsync()
        {
            UserContext newContext;
            try
            {
                // acquire token silent
                newContext = await AcquireToken();
            }
            catch (MsalUiRequiredException)
            {
                // acquire token interactive
                newContext = await SignInInteractively();
            }
            return newContext;
        }

        private async Task<UserContext> AcquireToken()
        {
            IEnumerable<IAccount> accounts = await _pca.GetAccountsAsync();
            AuthenticationResult authResult = await _pca.AcquireTokenSilent(B2CConstants.Scopes, GetAccountByPolicy(accounts, B2CConstants.PolicySignUpSignIn))
               .WithB2CAuthority(B2CConstants.AuthoritySignInSignUp)
               .ExecuteAsync();

            var newContext = UpdateUserInfo(authResult);
            return newContext;
        }
        private async Task<UserContext> SignInInteractively()
        {
            IEnumerable<IAccount> accounts = await _pca.GetAccountsAsync();

            AuthenticationResult authResult = await _pca.AcquireTokenInteractive(B2CConstants.Scopes)
                .WithPrompt(Prompt.SelectAccount)
                .WithAccount(GetAccountByPolicy(accounts, B2CConstants.PolicySignUpSignIn))
                .ExecuteAsync();

            var newContext = UpdateUserInfo(authResult);            
            return newContext;
        }
        private IAccount GetAccountByPolicy(IEnumerable<IAccount> accounts, string policy)
        {
            foreach (var account in accounts)
            {
                string userIdentifier = account.HomeAccountId.ObjectId.Split('.')[0];
                if (userIdentifier.EndsWith(policy.ToLower())) return account;
            }

            return null;
        }

Expected behavior

After first login, I think _pca.GetAccountsAsync() should return accounts list and be able to refresh token

Actual behavior

always _pca.GetAccountsAsync() returns empty array so, can't refresh token
So it ask to login every time app launches.

B2C Investigate answered no-repro question

All 16 comments

@tal-athena can you provide a repro or a link? Are you using the Xamarin B2C sample? If so, which platform are you using?

@jennyf19 , I used https://github.com/Azure-Samples/active-directory-b2c-xamarin-native
Xamarin Forms.
I tested it on Android

@jmprieur , Is the account saved by default when login success? should I add cache serialization to save account data?

thanks @tal-athena we will take a look. Did you change anything in the sample or run it as-is?

@jennyf19 , almost didn't changed, but the authentication code is the code provided

@tal-athena I'm not able to repro this. I do have an account in the cache:
image
My repro steps are:

  • on a Pixel 2 device
  • sign-in interactively on first app start
  • close the app
  • run app again, and on AT silent, i have an account in the cache.

@jennyf19 , can you provide me full project?
the sample repo also not working for me.
Do I need to do additional configuration in Azure or do sth in code?

@tal-athena i just did a git clone of master. the sample works as-is without any configuration changes. Are you using a device or simulator?

@jennyf19 , I am using simulator- google pixel-2 Pie 9.0

@tal-athena okay. thanks for the additional info. any chance you can try on a device? there are some slight performance differences between the simulator and device, as noted in our native android team's wiki. I'll try it out w/the simulator.

@jennyf19 , I also tested it on device (Samsung galaxy) but also not cached. I tested with sample repo,
does it need any additional configuration in azure?

@tal-athena no, you don't need to do anything, as the end-user. the sample should work as-is, with a git clone.
@trwalke @neha-bhargava could you try out the above sample as well? I'm not able to repro this issue atm. thank you.

@tal-athena - please work with Microsoft support to get your further, we cannot repro the issue

thanks for your attention
I resolved this error after adding openid scope to my app.
Thanks

Thanks for getting back to us @tal-athena !

Was this page helpful?
0 / 5 - 0 ratings