/// <summary>
        /// Remove a user from token cache.
        /// </summary>
        /// <param name="account"></param>
        /// <param name="tokenCache">This parameter is no longer used. However to keep the API unchanged it's not removed.</param>
        public void RemoveUser(IAzureAccount account, IAzureTokenCache tokenCache)
        {
            if (account != null && !string.IsNullOrEmpty(account.Id) && !string.IsNullOrWhiteSpace(account.Type))
            {
                switch (account.Type)
                {
                case AzureAccount.AccountType.AccessToken:
                    account.SetProperty(AzureAccount.Property.AccessToken, null);
                    account.SetProperty(AzureAccount.Property.GraphAccessToken, null);
                    account.SetProperty(AzureAccount.Property.KeyVaultAccessToken, null);
                    break;

                case AzureAccount.AccountType.ManagedService:
                    account.SetProperty(AzureAccount.Property.MSILoginUri, null);
                    break;

                case AzureAccount.AccountType.ServicePrincipal:
                    try
                    {
                        KeyStore.DeleteKey(account.Id, account.GetTenants().FirstOrDefault());
                    }
                    catch
                    {
                        // make best effort to remove credentials
                    }

                    RemoveFromTokenCache(account);
                    break;

                case AzureAccount.AccountType.User:
                    RemoveFromTokenCache(account);
                    break;
                }
            }
        }
        private IEnumerable <IAzureSubscription> ListSubscriptionsFromServer(IAzureAccount account, IAzureEnvironment environment, SecureString password, string promptBehavior)
        {
            string[] tenants = null;
            try
            {
                if (!account.IsPropertySet(AzureAccount.Property.Tenants))
                {
                    tenants = LoadAccountTenants(account, environment, password, promptBehavior);
                }
                else
                {
                    var storedTenants = account.GetPropertyAsArray(AzureAccount.Property.Tenants);
                    if (account.Type == AzureAccount.AccountType.User && storedTenants.Count() == 1)
                    {
                        TracingAdapter.Information(Resources.AuthenticatingForSingleTenant, account.Id, storedTenants[0]);
                        AzureSession.Instance.AuthenticationFactory.Authenticate(account, environment, storedTenants[0], password,
                                                                                 promptBehavior);
                    }
                }
            }
            catch (AadAuthenticationException aadEx)
            {
                WriteOrThrowAadExceptionMessage(aadEx);
                return(new AzureSubscription[0]);
            }

            try
            {
                tenants = tenants ?? account.GetTenants();
                List <IAzureSubscription> rdfeSubscriptions = ListServiceManagementSubscriptions(account, environment,
                                                                                                 password, ShowDialog.Never, tenants).ToList();

                // Set user ID
                foreach (var subscription in rdfeSubscriptions)
                {
                    account.SetOrAppendProperty(AzureAccount.Property.Subscriptions, subscription.Id.ToString());
                }

                if (rdfeSubscriptions.Any())
                {
                    return(rdfeSubscriptions);
                }
                else
                {
                    return(new AzureSubscription[0]);
                }
            }
            catch (AadAuthenticationException aadEx)
            {
                WriteOrThrowAadExceptionMessage(aadEx);
                return(new AzureSubscription[0]);
            }
        }
示例#3
0
        private static string GetCommonTenant(IAzureAccount account)
        {
            string result = AzureEnvironmentConstants.CommonAdTenant;

            if (account.IsPropertySet(AzureAccount.Property.Tenants))
            {
                var candidate = account.GetTenants().FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(candidate))
                {
                    result = candidate;
                }
            }

            return(result);
        }
        /// <summary>
        /// Get the common tenant from account
        /// </summary>
        /// <param name="account">The account to get tenant from</param>
        public static string GetCommonTenant(this IAzureAccount account)
        {
            string result = "organizations";

            if (account.IsPropertySet(AzureAccount.Property.Tenants))
            {
                var candidate = account.GetTenants().FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(candidate))
                {
                    result = candidate;
                }
            }

            return(result);
        }