@@ -54,7 +54,8 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
54
54
// (tenant is present and subscription is not provided)
55
55
if ( ! string . IsNullOrEmpty ( tenantId ) )
56
56
{
57
- TryGetTenantSubscription ( account , environment , tenantId , subscriptionId , password , promptBehavior , out newSubscription , out newTenant ) ;
57
+ var token = AcquireAccessToken ( account , environment , tenantId , password , promptBehavior ) ;
58
+ TryGetTenantSubscription ( token , account , environment , tenantId , subscriptionId , out newSubscription , out newTenant ) ;
58
59
}
59
60
// (tenant is not provided and subscription is present) OR
60
61
// (tenant is not provided and subscription is not provided)
@@ -63,16 +64,21 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
63
64
foreach ( var tenant in ListAccountTenants ( account , environment , password , promptBehavior ) )
64
65
{
65
66
AzureTenant tempTenant ;
66
- if ( TryGetTenantSubscription ( account , environment , tenant . Id . ToString ( ) , subscriptionId , password , ShowDialog . Auto , out newSubscription , out tempTenant ) )
67
- {
67
+ AzureSubscription tempSubscription ;
68
+ var token = AcquireAccessToken ( account , environment , tenant . Id . ToString ( ) , password ,
69
+ ShowDialog . Auto ) ;
70
+ if ( newTenant == null && TryGetTenantSubscription ( token , account , environment , tenant . Id . ToString ( ) , subscriptionId , out tempSubscription , out tempTenant ) &&
71
+ newTenant == null )
72
+ {
68
73
newTenant = tempTenant ;
74
+ newSubscription = tempSubscription ;
69
75
}
70
76
}
71
77
}
72
78
73
79
if ( newSubscription == null )
74
80
{
75
- throw new PSInvalidOperationException ( "Subscription was not found." ) ;
81
+ throw new PSInvalidOperationException ( String . Format ( Properties . Resources . NoSubscriptionFound , account . Id ) ) ;
76
82
}
77
83
78
84
_profile . Context = new AzureContext ( newSubscription , account , environment , newTenant ) ;
@@ -135,12 +141,14 @@ public bool TryGetSubscription(string tenantId, string subscriptionId, out Azure
135
141
{
136
142
if ( string . IsNullOrWhiteSpace ( tenantId ) )
137
143
{
138
- throw new ArgumentNullException ( "Please provide a valid tenant Id" ) ;
144
+ throw new ArgumentNullException ( "Please provide a valid tenant Id. " ) ;
139
145
}
140
146
141
147
AzureTenant tenant ;
142
- return TryGetTenantSubscription ( _profile . Context . Account , _profile . Context . Environment ,
143
- tenantId , subscriptionId , null , ShowDialog . Never , out subscription , out tenant ) ;
148
+ var token = AcquireAccessToken ( _profile . Context . Account , _profile . Context . Environment ,
149
+ tenantId , null , ShowDialog . Never ) ;
150
+ return TryGetTenantSubscription ( token , _profile . Context . Account , _profile . Context . Environment ,
151
+ tenantId , subscriptionId , out subscription , out tenant ) ;
144
152
}
145
153
146
154
public AzureEnvironment AddOrSetEnvironment ( AzureEnvironment environment )
@@ -235,23 +243,30 @@ private AzureEnvironment MergeEnvironmentProperties(AzureEnvironment environment
235
243
return mergedEnvironment ;
236
244
}
237
245
238
- private bool TryGetTenantSubscription (
239
- AzureAccount account ,
240
- AzureEnvironment environment ,
241
- string tenantId ,
242
- string subscriptionId ,
243
- SecureString password ,
244
- ShowDialog promptBehavior ,
246
+ private IAccessToken AcquireAccessToken ( AzureAccount account ,
247
+ AzureEnvironment environment ,
248
+ string tenantId ,
249
+ SecureString password ,
250
+ ShowDialog promptBehavior )
251
+ {
252
+ return AzureSession . AuthenticationFactory . Authenticate (
253
+ account ,
254
+ environment ,
255
+ tenantId ,
256
+ password ,
257
+ promptBehavior ,
258
+ TokenCache . DefaultShared ) ;
259
+ }
260
+
261
+ private bool TryGetTenantSubscription ( IAccessToken accessToken ,
262
+ AzureAccount account ,
263
+ AzureEnvironment environment ,
264
+ string tenantId ,
265
+ string subscriptionId ,
245
266
out AzureSubscription subscription ,
246
267
out AzureTenant tenant )
247
268
{
248
- var accessToken = AzureSession . AuthenticationFactory . Authenticate (
249
- account ,
250
- environment ,
251
- tenantId ,
252
- password ,
253
- promptBehavior ,
254
- TokenCache . DefaultShared ) ;
269
+
255
270
using ( var subscriptionClient = AzureSession . ClientFactory . CreateCustomClient < SubscriptionClient > (
256
271
new TokenCloudCredentials ( accessToken . AccessToken ) ,
257
272
environment . GetEndpointAsUri ( AzureEnvironment . Endpoint . ResourceManager ) ) )
@@ -311,13 +326,8 @@ private bool TryGetTenantSubscription(
311
326
312
327
private List < AzureTenant > ListAccountTenants ( AzureAccount account , AzureEnvironment environment , SecureString password , ShowDialog promptBehavior )
313
328
{
314
- var commonTenantToken = AzureSession . AuthenticationFactory . Authenticate (
315
- account ,
316
- environment ,
317
- AuthenticationFactory . CommonAdTenant ,
318
- password ,
319
- promptBehavior ,
320
- TokenCache . DefaultShared ) ;
329
+ var commonTenantToken = AcquireAccessToken ( account , environment , AuthenticationFactory . CommonAdTenant ,
330
+ password , promptBehavior ) ;
321
331
322
332
using ( var subscriptionClient = AzureSession . ClientFactory . CreateCustomClient < SubscriptionClient > (
323
333
new TokenCloudCredentials ( commonTenantToken . AccessToken ) ,
@@ -341,13 +351,7 @@ public IEnumerable<AzureTenant> ListTenants()
341
351
private IEnumerable < AzureSubscription > ListSubscriptionsForTenant ( AzureAccount account , AzureEnvironment environment ,
342
352
SecureString password , ShowDialog promptBehavior , string tenantId )
343
353
{
344
- var accessToken = AzureSession . AuthenticationFactory . Authenticate (
345
- account ,
346
- environment ,
347
- tenantId ,
348
- password ,
349
- promptBehavior ,
350
- TokenCache . DefaultShared ) ;
354
+ var accessToken = AcquireAccessToken ( account , environment , tenantId , password , promptBehavior ) ;
351
355
using ( var subscriptionClient = AzureSession . ClientFactory . CreateCustomClient < SubscriptionClient > (
352
356
new TokenCloudCredentials ( accessToken . AccessToken ) ,
353
357
environment . GetEndpointAsUri ( AzureEnvironment . Endpoint . ResourceManager ) ) )
0 commit comments