diff --git a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs index aa73d1e6a522..7a7404c508ce 100644 --- a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs +++ b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs @@ -116,6 +116,40 @@ public void TokenIdAndAccountIdMismatch() Assert.Equal(1, tenantsInAccount.Length); Assert.Equal(tenants.First(), tenantsInAccount[0]); } + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void AdalExceptionsArePropagatedToCaller() + { + var tenants = new List { Guid.NewGuid().ToString(), DefaultTenant.ToString() }; + var secondsubscriptionInTheFirstTenant = Guid.NewGuid().ToString(); + var firstList = new List { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant }; + var secondList = new List { Guid.NewGuid().ToString() }; + var thirdList = new List { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant }; + var fourthList = new List { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant }; + var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList); + + var tokens = new Queue(); + tokens.Enqueue(new MockAccessToken + { + UserId = "aaa@contoso.com", + LoginType = LoginType.OrgId, + AccessToken = "bbb" + }); + + ((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) => + { + throw new AadAuthenticationCanceledException("Login window was closed", null); + }; + + Assert.Throws( () => client.Login( + Context.Account, + Context.Environment, + null, + secondsubscriptionInTheFirstTenant, + null, + null)); + } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] diff --git a/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs b/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs index f2eb97775195..e7ba73bd1f46 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs @@ -386,9 +386,10 @@ public IEnumerable ListSubscriptions() } catch (AadAuthenticationException) { - WriteWarningMessage(string.Format("Could not authenticate user account {0} with tenant {1}. " + - "Subscriptions in this tenant will not be listed. Please login again using Login-AzureRmAccount " + - "to view the subscriptions in this tenant.", _profile.Context.Account, tenant)); + WriteWarningMessage(string.Format( + Microsoft.Azure.Commands.Profile.Properties.Resources.UnableToLogin, + _profile.Context.Account, + tenant)); } } @@ -576,11 +577,12 @@ private List ListAccountTenants(AzureAccount account, AzureEnvironm { result = account.GetPropertyAsArray(AzureAccount.Property.Tenants) - .Select( ti => { + .Select(ti => + { var tenant = new AzureTenant(); - + Guid guid; - if(Guid.TryParse(ti, out guid)) + if (Guid.TryParse(ti, out guid)) { tenant.Id = guid; tenant.Domain = AccessTokenExtensions.GetDomain(account.Id); @@ -593,8 +595,12 @@ private List ListAccountTenants(AzureAccount account, AzureEnvironm return tenant; }).ToList(); } - - } + if(!result.Any()) + { + throw; + } + + } return result; } @@ -640,7 +646,7 @@ private void WriteWarningMessage(string message) WarningLog(message); } } - + private static AzureTenant CreateTenantFromString(string tenantOrDomain, string accessTokenTenantId) { AzureTenant result = new AzureTenant(); diff --git a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs index 25b66affd3a4..19595829b2cd 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs @@ -205,7 +205,7 @@ internal static string TypeNotAccessToken { } /// - /// Looks up a localized string similar to Unabe to acquire token for tenant '{0}'. + /// Looks up a localized string similar to Unable to acquire token for tenant '{0}'. /// internal static string UnableToAqcuireToken { get { @@ -213,6 +213,15 @@ internal static string UnableToAqcuireToken { } } + /// + /// Looks up a localized string similar to Could not authenticate user account '{0}' with tenant '{1}'. Subscriptions in this tenant will not be listed. Please login again using Login-AzureRmAccount to view the subscriptions in this tenant.. + /// + internal static string UnableToLogin { + get { + return ResourceManager.GetString("UnableToLogin", resourceCulture); + } + } + /// /// Looks up a localized string similar to Unable to find environment with name '{0}'. /// diff --git a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx index 4c96b535778b..f78e48e6668c 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx +++ b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx @@ -168,7 +168,10 @@ Unable to acquire token for tenant '{0}' + + Could not authenticate user account '{0}' with tenant '{1}'. Subscriptions in this tenant will not be listed. Please login again using Login-AzureRmAccount to view the subscriptions in this tenant. + Unable to find environment with name '{0}' - + \ No newline at end of file