Implementing MSAL in MAUI application.
Sign In process is created by using below snippet.
authenticationResult = await _identityClient
.AcquireTokenInteractive(AppSettings.MSALScopes)
.WithUseEmbeddedWebView(true)
.ExecuteAsync();
When the first time application opens, The user will see the MS Sign In page. After sign in, The user will be redirected to the ‘MainPage’ (HomePage).
When that signed in user close & re-opens the application, He must be directly redirected to the ‘MainPage’ (HomePage). For that, I have below code to check if user has already signed in.
public AuthService()
{
_identityClient = PublicClientApplicationBuilder.Create(AppSettings.MSALAzureClientId)
.WithRedirectUri(AppSettings.MSALRedirectURI)
.WithCacheOptions(CacheOptions.EnableSharedCacheOptions)
.Build();
}
public async Task<bool> IsSignedInAsync()
{
try
{
_account = (await _identityClient?.GetAccountsAsync())?.FirstOrDefault();
var authenticationResult = await _identityClient.AcquireTokenSilent(AppSettings.MSALScopes, _account).ExecuteAsync();
return authenticationResult != null;
}
catch
{
return false;
}
}
But, When I am calling the ‘IsSignedInAsync’ method, ‘GetAccountsAsync’ returns 0 accounts even after the user has already signed in.
How can I get the accounts that have been already signed in?