@@ -61,15 +61,15 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
61
61
// (tenant is not provided and subscription is not provided)
62
62
else
63
63
{
64
- foreach ( var tenant in ListAccountTenants ( account , environment , password , promptBehavior ) )
64
+ foreach ( var tenant in ListAccountTenants ( account , environment , password , promptBehavior ) )
65
65
{
66
66
AzureTenant tempTenant ;
67
67
AzureSubscription tempSubscription ;
68
68
var token = AcquireAccessToken ( account , environment , tenant . Id . ToString ( ) , password ,
69
69
ShowDialog . Auto ) ;
70
70
if ( newTenant == null && TryGetTenantSubscription ( token , account , environment , tenant . Id . ToString ( ) , subscriptionId , out tempSubscription , out tempTenant ) &&
71
71
newTenant == null )
72
- {
72
+ {
73
73
newTenant = tempTenant ;
74
74
newSubscription = tempSubscription ;
75
75
}
@@ -93,8 +93,8 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
93
93
{
94
94
_profile . Context = new AzureContext (
95
95
_profile . Context . Subscription ,
96
- _profile . Context . Account ,
97
- _profile . Context . Environment ,
96
+ _profile . Context . Account ,
97
+ _profile . Context . Environment ,
98
98
new AzureTenant ( ) { Id = new Guid ( tenantId ) } ) ;
99
99
100
100
if ( _profile . Context . Account != null )
@@ -107,10 +107,17 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
107
107
}
108
108
}
109
109
110
- if ( ! string . IsNullOrWhiteSpace ( subscriptionId ) )
110
+ if ( ! string . IsNullOrWhiteSpace ( subscriptionId ) )
111
111
{
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
+
112
119
var newSubscription = new AzureSubscription { Id = new Guid ( subscriptionId ) } ;
113
- if ( _profile . Context . Subscription != null )
120
+ if ( _profile . Context . Subscription != null )
114
121
{
115
122
newSubscription . Account = _profile . Context . Subscription . Account ;
116
123
newSubscription . Environment = _profile . Context . Subscription . Environment ;
@@ -121,7 +128,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
121
128
_profile . Context = new AzureContext (
122
129
newSubscription ,
123
130
_profile . Context . Account ,
124
- _profile . Context . Environment ,
131
+ _profile . Context . Environment ,
125
132
_profile . Context . Tenant ) ;
126
133
}
127
134
@@ -131,7 +138,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
131
138
public List < AzureTenant > ListTenants ( string tenant )
132
139
{
133
140
return ListAccountTenants ( _profile . Context . Account , _profile . Context . Environment , null , ShowDialog . Auto )
134
- . Where ( t => tenant == null ||
141
+ . Where ( t => tenant == null ||
135
142
tenant . Equals ( t . Id . ToString ( ) , StringComparison . OrdinalIgnoreCase ) ||
136
143
tenant . Equals ( t . Domain , StringComparison . OrdinalIgnoreCase ) )
137
144
. ToList ( ) ;
@@ -279,7 +286,7 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
279
286
out AzureSubscription subscription ,
280
287
out AzureTenant tenant )
281
288
{
282
-
289
+
283
290
using ( var subscriptionClient = AzureSession . ClientFactory . CreateCustomClient < SubscriptionClient > (
284
291
new TokenCloudCredentials ( accessToken . AccessToken ) ,
285
292
environment . GetEndpointAsUri ( AzureEnvironment . Endpoint . ResourceManager ) ) )
@@ -339,15 +346,15 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
339
346
340
347
private List < AzureTenant > ListAccountTenants ( AzureAccount account , AzureEnvironment environment , SecureString password , ShowDialog promptBehavior )
341
348
{
342
- var commonTenantToken = AcquireAccessToken ( account , environment , AuthenticationFactory . CommonAdTenant ,
349
+ var commonTenantToken = AcquireAccessToken ( account , environment , AuthenticationFactory . CommonAdTenant ,
343
350
password , promptBehavior ) ;
344
351
345
352
using ( var subscriptionClient = AzureSession . ClientFactory . CreateCustomClient < SubscriptionClient > (
346
353
new TokenCloudCredentials ( commonTenantToken . AccessToken ) ,
347
354
environment . GetEndpointAsUri ( AzureEnvironment . Endpoint . ResourceManager ) ) )
348
355
{
349
356
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 ( ) } )
351
358
. ToList ( ) ;
352
359
}
353
360
}
@@ -361,7 +368,7 @@ public IEnumerable<AzureTenant> ListTenants()
361
368
return ListAccountTenants ( _profile . Context . Account , _profile . Context . Environment , null , ShowDialog . Never ) ;
362
369
}
363
370
364
- private IEnumerable < AzureSubscription > ListSubscriptionsForTenant ( AzureAccount account , AzureEnvironment environment ,
371
+ private IEnumerable < AzureSubscription > ListSubscriptionsForTenant ( AzureAccount account , AzureEnvironment environment ,
365
372
SecureString password , ShowDialog promptBehavior , string tenantId )
366
373
{
367
374
var accessToken = AcquireAccessToken ( account , environment , tenantId , password , promptBehavior ) ;
0 commit comments