Пример #1
0
        public async Task TestCallIdentityService()
        {
            // Get auth token and identity url (through refresh)
            var account = await OAuth2.RefreshAuthTokenAsync(TestCredentials.TestAccount);

            // Call the identity service
            IdentityResponse identityResponse = await OAuth2.CallIdentityServiceAsync(account.IdentityUrl, account.AccessToken);

            // Check username
            Assert.AreEqual(TestCredentials.Username, identityResponse.UserName);
        }
Пример #2
0
        /// <summary>
        ///     Create and persist Account for newly authenticated user
        /// </summary>
        /// <param name="loginOptions"></param>
        /// <param name="authResponse"></param>
        /// <param name="cancellationToken"></param>
        public static async Task <Account> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse,
                                                            CancellationToken cancellationToken = default(CancellationToken))
        {
            LoggingService.Log("Create account object", LoggingLevel.Verbose);

            var account = new Account(
                loginOptions.LoginUrl,
                loginOptions.ClientId,
                loginOptions.CallbackUrl,
                loginOptions.Scopes,
                authResponse.InstanceUrl,
                authResponse.IdentityUrl,
                authResponse.AccessToken,
                authResponse.RefreshToken)
            {
                CommunityId  = authResponse.CommunityId,
                CommunityUrl = authResponse.CommunityUrl
            };

            var cm = new ClientManager();

            cm.PeekRestClient();
            IdentityResponse identity = null;

            try
            {
                identity =
                    await
                    OAuth2.CallIdentityServiceAsync(authResponse.IdentityUrl, authResponse.AccessToken,
                                                    cancellationToken);
            }
            catch (JsonException ex)
            {
                LoggingService.Log(ex, LoggingLevel.Critical, "Exception occurred when retrieving account identity");
                Debug.WriteLine("Error retrieving account identity");
            }

            if (identity != null)
            {
                account.UserId         = identity.UserId;
                account.UserName       = identity.UserName;
                account.Policy         = identity.MobilePolicy;
                account.OrganizationId = identity.OrganizationId;

                await AuthStorageHelper.PersistCurrentAccountAsync(account);

                LoggedInAccount = account;
            }
            LoggingService.Log("Finished creating account", LoggingLevel.Verbose);
            return(account);
        }
Пример #3
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);
        }