2
votes

I have a Xamarin.Forms iOS/Android app and have to authenticate the user against a Microsoft Azure B2C AD.

I have added the Microsoft.Identity.Client NuGet and using this for authentication.

Login works fine and I am able to use the returned token to call an Azure hosted service of our own.

The trouble is that when I try to logout the user it does not work as expected.

If the user logs out and immediately after kills the app, the next time the app is started the login screen is presented as expected.

To kill the app on iOS I go to app-switcher with double click home button and the swipe up.

But if the user does NOT kill the app, but instead presses my login button (triggers a call to AcquireTokenAsync()) the sign in screen is NOT presented by Microsoft.Identity.Client but instead a valid token is immediately returned and the app is able to use this token for service calls, i.e. it is a valid token.

Next time the app is launched the token is no longer there and the login screen appear.

The strange this is that when I run the sample from GitHub/active-directory-b2c-xamarin-native I see the same behaviour. So I suspect it is a bug in the MSAL component from Microsoft.

According to the sample logout should simply be done with calling

PublicClientApplication.UserTokenCache.Clear(PublicClientApplication.ClientId);

I have also tried with adding

foreach (var user in PublicClientApplication.Users)
{
     user.SignOut();
}

without any change.

Any suggestions?

2
I just changed the UiOptions parameter to AcquireTokenAsync() from UiOptions.SelectAccount to UiOptions.ForceLogin and it is a bit more inconvenient for the user but it does ensure the login screen is shown.Nicolai Henriksen

2 Answers

3
votes

Just add following line in Droid Project:

CookieManager.Instance.RemoveAllCookie();

in iOS,

 foreach (var cookie in NSHttpCookieStorage.SharedStorage.Cookies)
 {
   NSHttpCookieStorage.SharedStorage.DeleteCookie (cookie);
 }

and for more details visit this link https://developer.xamarin.com/guides/xamarin-forms/cloud-services/authentication/azure/

1
votes

This is happening because the service does not support certain features that would result in a user sign out. This is still a work in progress.