question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Investigate] B2C Xamarin Sample getAccounts does not return an account after first sign-in

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tal-athenacommented, Oct 16, 2020

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

1reaction
tal-athenacommented, Oct 15, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not Returning from B2C Login - xamarin
I'm trying to follow this guide against my tenant but after I sign-in, I'm not returning to my Android Xamarin app. I've updated...
Read more >
Authenticate user in xamarin using AD B2C
I tried this sample https://t.co/E1wfsbGcHa. However, after login it goes back to the login page instead of the login result page where the ......
Read more >
User Authentication in Xamarin with Azure AD B2C
In this post we'll examine what you need to do to implement Azure AD B2C ... that we want to collect and return...
Read more >
Authenticate Users with Azure Active Directory B2C
It allows users to sign in to your application using their existing social accounts or custom credentials such as email or username, and ......
Read more >
Xamarin + Azure Active Directory B2C - Matt Soucoup
Add Azure Active Directory B2C authentication to your Xamarin app. Recorded February 13, 2018 For more great, cross-platform mobile ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found