Ryvik
Ryvik

Reputation: 473

Getting a "cannot convert" error when trying to used a cached token

I'm getting the following error when trying to use a cached token. I'm using the list that was provided as an example here: https://learn.microsoft.com/bs-latn-ba/azure/active-directory/develop/msal-net-acquire-token-silently

cannot convert from 'System.Collections.Generic.IEnumerable' to 'Microsoft.Identity.Client.IAccount'`

        static async Task<GraphServiceClient> Auth()
        {

            var clientApp = PublicClientApplicationBuilder.Create(ConfigurationManager.AppSettings["clientId"]).Build();
            string[] scopes = new string[] { "user.read" };
            string token = null;
            var app = PublicClientApplicationBuilder.Create(ConfigurationManager.AppSettings["clientId"]).Build();
            AuthenticationResult result = null;
            var accounts = await app.GetAccountsAsync();

                result = await app.AcquireTokenSilent(scopes, accounts)
                       .ExecuteAsync();
                token = result.AccessToken;

            GraphServiceClient graphClient = new GraphServiceClient(
                        "https://graph.microsoft.com/v1.0",
                        new DelegateAuthenticationProvider(
                            async (requestMessage) =>
                            {
                                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
                            }));
            return graphClient;
        }

Upvotes: 0

Views: 551

Answers (1)

Jawad
Jawad

Reputation: 11364

The call that is causing the error is this one

result = await app.AcquireTokenSilent(scopes, accounts)
                  .ExecuteAsync();

AcquireTokenSilent requires two variables, IEnumerable<String> (scops) and string (loginHint). What you are passing for loginHist is an array of accounts when it's supposed to be a single string (single account).

var accounts = await app.GetAccountsAsync();

GetAccountsAsync()'s return type is IEnumerable which is why its throwing the error at that call.

In an example here, the account looked up is from the list.

// Get the account
IAccount account = await application.GetAccountAsync(accountIdentifier).ConfigureAwait(false);

// Special case for guest users as the Guest oid / tenant id are not surfaced.
if (account == null)
{
    if (loginHint == null)
        throw new ArgumentNullException(nameof(loginHint));

    var accounts = await application.GetAccountsAsync().ConfigureAwait(false);
    account = accounts.FirstOrDefault(a => a.Username == loginHint);
}

Add this line to your code:

var accounts = await app.GetAccountsAsync();
IAccount account = accounts.FirstOrDefault(a => a.Username == loginHint); // <--- Get Account from accounts
result = await app.AcquireTokenSilent(scopes, account) // Use account instead of accounts.
                  .ExecuteAsync();
token = result.AccessToken;

Upvotes: 1

Related Questions