GetAccountsAsync always returning an empty list

Viewed 220

I am trying to silently acquire a token, but the list of accounts is always empty. However, when I switch to acquiring the token interactively, then I always get at least the windows default account.

So how can I get at least the default account for a silent login?

Here is my code:

string ClientID = "xxx";
string TenantID = "yyy";

IPublicClientApplication pca = PublicClientApplicationBuilder.Create(ClientID)
        .WithAuthority(AzureCloudInstance.AzurePublic, TenantID)
        .WithRedirectUri(
            "https://login.microsoftonline.com/common/oauth2/nativeclient")
        .Build();

string[] scopes = { "user.read" };

List<IAccount> accounts = (List<IAccount>)await pca.GetAccountsAsync();
AuthenticationResult result;


if (accounts.Count > 0) // <--- ALWAYS ZERO
    result = await pca.AcquireTokenSilent(scopes, accounts[0]).ExecuteAsync();
else
    result = await pca.AcquireTokenInteractive(scopes).ExecuteAsync();

string token = result.AccessToken;
0 Answers
Related