Exemple #1
0
        public static async Task <bool> SwitchToAccountAsync(Account newAccount,
                                                             CancellationToken cancellationToken = default(CancellationToken))
        {
            var oldAccount = LoggedInAccount;

            if (newAccount?.UserId != null)
            {
                // save current user pin timer
                AuthStorageHelper.SavePinTimer();

                await AuthStorageHelper.PersistCurrentAccountAsync(newAccount);

                var client = SDKManager.GlobalClientManager.PeekRestClient();
                if (client != null)
                {
                    await AuthStorageHelper.ClearCookiesAsync(newAccount.GetLoginOptions());

                    var identity =
                        await OAuth2.CallIdentityServiceAsync(newAccount.IdentityUrl, client, cancellationToken);

                    if (identity != null)
                    {
                        newAccount.UserId   = identity.UserId;
                        newAccount.UserName = identity.UserName;
                        newAccount.Policy   = identity.MobilePolicy;
                        await AuthStorageHelper.PersistCurrentAccountAsync(newAccount);
                    }
                    AuthStorageHelper.RefreshCookies();
                    LoggingService.Log("switched accounts, result = true", LoggingLevel.Verbose);
                    return(true);
                }

                // log new user in
                LoggedInAccount = newAccount;

                RaiseAuthenticatedAccountChangedEvent(oldAccount, newAccount);
            }

            LoggingService.Log("switched accounts, result = false", LoggingLevel.Verbose);
            return(false);
        }