Hello,
The application can run as expected when using pca certification in the following way.
public class AuthService
{
private readonly IPublicClientApplication _pca;
private readonly string[] _scopes = new[] { "User.Read" }; // Change as needed
private IAccount _userAccount;
public AuthService()
{
#if ANDROID
_pca = PublicClientApplicationBuilder.Create("
.WithAuthority(AzureCloudInstance.AzurePublic, "
.WithRedirectUri("msauth://com.companyname.mauiapp1/") // Varies by platform
.WithParentActivityOrWindow(() => Platform.CurrentActivity) // Android platform specific line
.Build();
#else
_pca = PublicClientApplicationBuilder.Create("_clientId")
.WithAuthority(AzureCloudInstance.AzurePublic, "{_tenantId}")
.WithRedirectUri("msauth://com.companyname.mauiapp1/") // Varies by platform
.Build();
#endif
}
public async Task<AuthenticationResult> LoginAsync()
{
try
{
_userAccount = (await _pca.GetAccountsAsync()).FirstOrDefault();
return await _pca.AcquireTokenSilent(_scopes, _userAccount).ExecuteAsync();
}
catch (MsalUiRequiredException)
{
return await _pca.AcquireTokenInteractive(_scopes).ExecuteAsync();
}
}
public async Task LogoutAsync()
{
_userAccount = (await _pca.GetAccountsAsync()).FirstOrDefault();
if (_userAccount != null)
{
await _pca.RemoveAsync(_userAccount);
}
}
}
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.