+1 vote

Following the steps from:
https://www.limilabs.com/blog/oauth2-office365-exchange-imap-pop3-smtp

When I executed this code:

var account = (await app.GetAccountsAsync()).FirstOrDefault(); 

The account is always null what is the main problem?

In addition, this is the code that initiates the "app" variable:

var app = PublicClientApplicationBuilder
    .Create(clientId)
    .WithTenantId(tenantId)
    .WithDefaultRedirectUri()
    .Build();

// This allows saving access/refresh tokens to some storage
TokenCacheHelper.EnableSerialization(app.UserTokenCache);
by (780 points)

1 Answer

0 votes

GetAccountsAsync returns all the available accounts in the user token cache for the application. It's obvious it will return empty collection on first run.

In the sample mentioned there is a very simplistic cache implementation provided: TokenCacheHelper. It stores the cache on disk in the "msalcache.bin3" file.
You can view contents of this file to check what is saved in it.

There is a chance that you delete this file when compiling the app.

Please note that most likely you should store this cache in an encrypted form in some kind of a database. Consider using MSAL token serialization implementations available here:

https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-net-token-cache-serialization

by (300k points)
We are not MSAL .NET support. Have in mind that AcquireTokenInteractive creates a new browser window or displays a login dialog (depending on the configuration) - your application must not be a web app for example.
 
I think there is a WithLoginHint method, that lets you specify which account to use. Member will see a choose account dialog otherwise.

As far as I know Multitenant + Personal works for all flows (desktop, web, device).
Hello, good day.
do you have a sample using the "[x] Accounts in any organizational directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts" with an acquired method to get token?

The code is exactly the same. The only difference is

.WithAuthority( 
    AadAuthorityAudience.AzureAdAndPersonalMicrosoftAccount
)

instead of:

.WithTenantId(tenantId)
if so why does the desktop flow use the AcquireTokenInteractive
what settings do I need to make it work?
AcquireTokenInteractive is not working on my end.

I have tried the login hint as well but still not working.
Desktop flow uses AcquireTokenInteractive - that is correct.
I don't really understand your problem.
Are you developing a desktop app? What technology are you using?

I believe you should ask a new question to be honest.
Please note that AcquireTokenInteractive is part of MSAL .NET - Microsoft library.
...