Azure AD re-authentication using AcquireTokenSilentAsync

RachelCandaRachelCanda USUniversity
edited July 2017 in Xamarin.iOS

I'm working on an Azure AD plugin for iOS that allows you to log in, logout and re-authenticate. But I am having issues with the re-authentication function. I am trying to use AcquireTokenSilentAsync method to acquire the token without having the user enter their login credentials again but it is throwing the following exception:

Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException: Failed to acquire token silently as no token was found in the cache

I've not seen any examples of this method being used in an app, if anyone has a working example or any advice would be really helpful.

These are the methods that I'm using to Authenticate and to Re-Authenticate:

public class AzureADImplementation
{
        AuthenticationContext authContext;

        public async Task<AzureUser> Authenticate(string commonAuthority, string resourceId, string clientId, string redirectUri)
        {
            try
            {
                authContext = new AuthenticationContext(commonAuthority);

                if (authContext.TokenCache.ReadItems().Count() > 0)
                    authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);

                var platform = new PlatformParameters(GetCurrentViewController());

                var authResult = await authContext.AcquireTokenAsync(resourceId, clientId, new Uri(redirectUri), platform);

                var user = new AzureUser(){
                    UniqueId = authResult.UserInfo.UniqueId,
                    Firstname = authResult.UserInfo.GivenName,
                    Surname = authResult.UserInfo.FamilyName,
                    DisplayableId = authResult.UserInfo.DisplayableId,
                    IdentityProvider = authResult.UserInfo.IdentityProvider,
                    AccessToken = authResult.AccessToken,
                    IdToken = authResult.IdToken
                };

                return user;

            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public async Task<AzureUser> ReauthenticateSilently(string commonAuthority, string resourceId, string clientId)
        {
            try
            {
                authContext = new AuthenticationContext(commonAuthority);

                if (authContext.TokenCache.ReadItems().Count() > 0)
                    authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);

                var result = await authContext.AcquireTokenSilentAsync(resourceId, clientId);

                var user = new AzureUser()
                {
                    UniqueId = result.UserInfo.UniqueId,
                    Firstname = result.UserInfo.GivenName,
                    Surname = result.UserInfo.FamilyName,
                    DisplayableId = result.UserInfo.DisplayableId,
                    IdentityProvider = result.UserInfo.IdentityProvider,
                    AccessToken = result.AccessToken,
                    IdToken = result.IdToken
                };

                return user;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public UIViewController GetCurrentViewController()
        {
            var window = UIApplication.SharedApplication.KeyWindow;
            var vc = window.RootViewController;
            while (vc.PresentedViewController != null)
            {
                vc = vc.PresentedViewController;
            }

            return vc;
        }
    }

Answers

Sign In or Register to comment.