MSAL - PublicClientApplication - GetAccountsAsync() doesn't return any Accounts in windows MAUI
Vaibhav Methuku
80
Reputation points
The MAUI windows app MSAL - PublicClientApplication - GetAccountsAsync() doesn't return any Accounts.
I followed the process OS integrating the MSAL as per the Microsoft document.
Here is the code snippet:
try
{
IEnumerable accounts = await mPublicClientApplication.GetAccountsAsync();
IAccount firstAccount = accounts.FirstOrDefault();
result = await mPublicClientApplication.AcquireTokenSilent(mIndustrialAppsADOptions.Scopes, firstAccount).ExecuteAsync();
return result;
}
catch(MsalUiRequiredException msre)
{
try
{
if(Device.RuntimePlatform == Device.UWP)
{
result = await mPublicClientApplication.AcquireTokenInteractive(mIndustrialAppsADOptions.Scopes) .WithUseEmbeddedWebView(true).ExecuteAsync();
else
{
result = await mPublicClientApplication.AcquireTokenInteractive(mIndustrialAppsADOptions.Scopes) .WithUseEmbeddedWebView(true) .WithParentActivityOrWindow(RootViewController).ExecuteAsync(); }
return result;
}
catch(MsalException ex)
{
return null;
}
catch(Exception exc)
{
return null;
}
}
` public void RegisterTypes(IContainerRegistry containerRegistry)
{
IIndustrialAppsADOptions options = ((IContainerProvider)containerRegistry).Resolve();
string authority = options.Authority;
if(Device.RuntimePlatform == Device.UWP)
{
mPublicClientApplication = PublicClientApplicationBuilder.Create(options.ClientId)
.WithAuthority(authority)
.WithRedirectUri(SpecialRedirectUri?? $"msal{options.ClientId}://auth")
.Build();
}
else if(Device.RuntimePlatform == Device.Android)
{
mPublicClientApplication = PublicClientApplicationBuilder.Create(options.ClientId)
.WithAuthority(authority)
.WithRedirectUri($"msal{options.ClientId}://auth")
.Build();
}
else
{
mPublicClientApplication = PublicClientApplicationBuilder.Create(options.ClientId)
.WithAuthority(authority)
.WithIosKeychainSecurityGroup(AppSettingsManager.Settings["IOS_KEYCHAIN_SECURITY_GROUP"]) //The ("*") will be replaced with ("com.microsoft.adalcache") during Production release
.WithRedirectUri($"msal{options.ClientId}://auth")
.Build();
}
containerRegistry.RegisterInstance(mPublicClientApplication);
containerRegistry.Register<IAuthenticationService, AuthenticationService>();
}`
Steps to Reproduce
- Create MAUI app
- Add the MSAL authentication
- When user Runs the application in Windows Platform for the first time Application asks for the MSAL login -> User enters the credentials and validates successfully
- Close the app
- Second time when user opens the app, it is again asking for the MSAL login.
Expected Behavior :
It should not ask for the Login again. Instead, It should get the Account information which is previously logged in.
Actual Behavior :
It is asking the user MSAL authentication every time when open the application.
But the same is working fine in the Android Platform.
It is only broken in Windows Platform