ConfidentialClientApplication GetAccountsAsync returns nothing- OAuth2.0 and Imap

Neha0408 31 Reputation points
2022-10-19T20:29:38.667+00:00

I'm following auth code flow to get access mailboxes in my webapp

In the first attempt I'm using-

var app= ConfidentialClientApplicationBuilder.Create("ClientID").WithAuthority(String.Format(authority, "tenantID")).WithClientSecret("ClientSecret").WithRedirectUri("RedirectUri").Build()
app.AddInMemoryTokenCache()

var result= app.AcquireTokenByAuthorizationCode(Scopes, authCode).ExecuteAsync()

taking note of the account identifier here-
string identifier = result.Account.HomeAccountId.Identifier

This works fine in first pass and I assume the token gets stored in cache for subsequent uses.

On the second attempt, Im trying to get the refresh token-

var app= ConfidentialClientApplicationBuilder.Create("ClientID").WithAuthority(String.Format(authority, "tenantID")).WithClientSecret("ClientSecret").WithRedirectUri("RedirectUri").Build()

var account= Await app.GetAccountsAsync(openIdentifier).FirstOrDefault()

AuthenticationResult refresh = Await app.AcquireTokenSilent(scopes, account).WithForceRefresh(True).ExecuteAsync()

The account is always null. Please suggest what I might be missing. I cannot use PublicClientApplicationBuilder because its a asp.net web forms app.

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,664 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Shweta Mathur 27,936 Reputation points Microsoft Employee
    2022-10-28T07:04:53.34+00:00

    Hi @Neha0408 ,

    Thanks for reaching out and apologies for delay in response.

    GetAccountsAsync returns all the available accounts in the user token cache for the application.

    As mentioned in the code, in the second attempt, you are initiating the "app" variable again and calling GetAccountsAsync() which is running first time after app initialization and nothing in cache.

    If your requirement is to instantiate a new app on every action, then it would be recommended to implement serialized token cache.

    Hope this will help.

    Thanks,
    Shweta

    ---------------------------------------

    Please remember to "Accept Answer" if answer helped you.

    0 comments No comments