@@ -61,15 +61,15 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
6161 // (tenant is not provided and subscription is not provided)
6262 else
6363 {
64- foreach ( var tenant in ListAccountTenants ( account , environment , password , promptBehavior ) )
64+ foreach ( var tenant in ListAccountTenants ( account , environment , password , promptBehavior ) )
6565 {
6666 AzureTenant tempTenant ;
6767 AzureSubscription tempSubscription ;
6868 var token = AcquireAccessToken ( account , environment , tenant . Id . ToString ( ) , password ,
6969 ShowDialog . Auto ) ;
7070 if ( newTenant == null && TryGetTenantSubscription ( token , account , environment , tenant . Id . ToString ( ) , subscriptionId , out tempSubscription , out tempTenant ) &&
7171 newTenant == null )
72- {
72+ {
7373 newTenant = tempTenant ;
7474 newSubscription = tempSubscription ;
7575 }
@@ -93,8 +93,8 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
9393 {
9494 _profile . Context = new AzureContext (
9595 _profile . Context . Subscription ,
96- _profile . Context . Account ,
97- _profile . Context . Environment ,
96+ _profile . Context . Account ,
97+ _profile . Context . Environment ,
9898 new AzureTenant ( ) { Id = new Guid ( tenantId ) } ) ;
9999
100100 if ( _profile . Context . Account != null )
@@ -107,10 +107,17 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
107107 }
108108 }
109109
110- if ( ! string . IsNullOrWhiteSpace ( subscriptionId ) )
110+ if ( ! string . IsNullOrWhiteSpace ( subscriptionId ) )
111111 {
112+ if ( ListSubscriptions ( _profile . Context . Tenant . Id . ToString ( ) ) . FirstOrDefault ( s =>
113+ String . Compare ( s . Id . ToString ( ) , subscriptionId , StringComparison . OrdinalIgnoreCase ) == 0 ) == null )
114+ {
115+ throw new ArgumentException ( string . Format (
116+ "Provided subscription {0} does not exist under current tenant {1}" , subscriptionId , _profile . Context . Tenant . Id ) ) ;
117+ }
118+
112119 var newSubscription = new AzureSubscription { Id = new Guid ( subscriptionId ) } ;
113- if ( _profile . Context . Subscription != null )
120+ if ( _profile . Context . Subscription != null )
114121 {
115122 newSubscription . Account = _profile . Context . Subscription . Account ;
116123 newSubscription . Environment = _profile . Context . Subscription . Environment ;
@@ -121,7 +128,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
121128 _profile . Context = new AzureContext (
122129 newSubscription ,
123130 _profile . Context . Account ,
124- _profile . Context . Environment ,
131+ _profile . Context . Environment ,
125132 _profile . Context . Tenant ) ;
126133 }
127134
@@ -131,7 +138,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
131138 public List < AzureTenant > ListTenants ( string tenant )
132139 {
133140 return ListAccountTenants ( _profile . Context . Account , _profile . Context . Environment , null , ShowDialog . Auto )
134- . Where ( t => tenant == null ||
141+ . Where ( t => tenant == null ||
135142 tenant . Equals ( t . Id . ToString ( ) , StringComparison . OrdinalIgnoreCase ) ||
136143 tenant . Equals ( t . Domain , StringComparison . OrdinalIgnoreCase ) )
137144 . ToList ( ) ;
@@ -279,7 +286,7 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
279286 out AzureSubscription subscription ,
280287 out AzureTenant tenant )
281288 {
282-
289+
283290 using ( var subscriptionClient = AzureSession . ClientFactory . CreateCustomClient < SubscriptionClient > (
284291 new TokenCloudCredentials ( accessToken . AccessToken ) ,
285292 environment . GetEndpointAsUri ( AzureEnvironment . Endpoint . ResourceManager ) ) )
@@ -339,15 +346,15 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
339346
340347 private List < AzureTenant > ListAccountTenants ( AzureAccount account , AzureEnvironment environment , SecureString password , ShowDialog promptBehavior )
341348 {
342- var commonTenantToken = AcquireAccessToken ( account , environment , AuthenticationFactory . CommonAdTenant ,
349+ var commonTenantToken = AcquireAccessToken ( account , environment , AuthenticationFactory . CommonAdTenant ,
343350 password , promptBehavior ) ;
344351
345352 using ( var subscriptionClient = AzureSession . ClientFactory . CreateCustomClient < SubscriptionClient > (
346353 new TokenCloudCredentials ( commonTenantToken . AccessToken ) ,
347354 environment . GetEndpointAsUri ( AzureEnvironment . Endpoint . ResourceManager ) ) )
348355 {
349356 return subscriptionClient . Tenants . List ( ) . TenantIds
350- . Select ( ti => new AzureTenant ( ) { Id = new Guid ( ti . TenantId ) , Domain = commonTenantToken . GetDomain ( ) } )
357+ . Select ( ti => new AzureTenant ( ) { Id = new Guid ( ti . TenantId ) , Domain = commonTenantToken . GetDomain ( ) } )
351358 . ToList ( ) ;
352359 }
353360 }
@@ -361,7 +368,7 @@ public IEnumerable<AzureTenant> ListTenants()
361368 return ListAccountTenants ( _profile . Context . Account , _profile . Context . Environment , null , ShowDialog . Never ) ;
362369 }
363370
364- private IEnumerable < AzureSubscription > ListSubscriptionsForTenant ( AzureAccount account , AzureEnvironment environment ,
371+ private IEnumerable < AzureSubscription > ListSubscriptionsForTenant ( AzureAccount account , AzureEnvironment environment ,
365372 SecureString password , ShowDialog promptBehavior , string tenantId )
366373 {
367374 var accessToken = AcquireAccessToken ( account , environment , tenantId , password , promptBehavior ) ;
0 commit comments