@@ -111,6 +111,11 @@ public bool TryRemoveContext(IAzureContext context)
111
111
return result ;
112
112
}
113
113
114
+ private IAzureTenant GetDetailedTenantFromQueryHistory ( List < AzureTenant > tenants , string tenantId )
115
+ {
116
+ return tenants ? . Where ( t => t . Id . Equals ( tenantId ) ) ? . FirstOrDefault ( ) ; ;
117
+ }
118
+
114
119
public AzureRmProfile Login (
115
120
IAzureAccount account ,
116
121
IAzureEnvironment environment ,
@@ -126,8 +131,9 @@ public AzureRmProfile Login(
126
131
int maxContextPopulation = Profile . ConnectAzureRmAccountCommand . DefaultMaxContextPopulation ,
127
132
string authScope = null )
128
133
{
129
- IAzureSubscription newSubscription = null ;
130
- IAzureTenant newTenant = null ;
134
+ IAzureSubscription defaultSubscription = null ;
135
+ IAzureTenant defaultTenant = null ;
136
+ List < AzureTenant > tenants = null ;
131
137
string promptBehavior =
132
138
( password == null &&
133
139
account . Type != AzureAccount . AccountType . AccessToken &&
@@ -151,15 +157,15 @@ public AzureRmProfile Login(
151
157
throw new PSInvalidOperationException ( Resources . SubscriptionOrTenantMissing ) ;
152
158
}
153
159
154
- newSubscription = new AzureSubscription
160
+ defaultSubscription = new AzureSubscription
155
161
{
156
162
Id = subscriptionId
157
163
} ;
158
164
159
- newSubscription . SetOrAppendProperty ( AzureSubscription . Property . Tenants , tenantIdOrName ) ;
160
- newSubscription . SetOrAppendProperty ( AzureSubscription . Property . Account , account . Id ) ;
165
+ defaultSubscription . SetOrAppendProperty ( AzureSubscription . Property . Tenants , tenantIdOrName ) ;
166
+ defaultSubscription . SetOrAppendProperty ( AzureSubscription . Property . Account , account . Id ) ;
161
167
162
- newTenant = new AzureTenant
168
+ defaultTenant = new AzureTenant
163
169
{
164
170
Id = tenantIdOrName
165
171
} ;
@@ -225,18 +231,17 @@ public AzureRmProfile Login(
225
231
subscriptionId ,
226
232
subscriptionName ,
227
233
true ,
228
- out newSubscription ,
229
- out newTenant ) )
234
+ out defaultSubscription ,
235
+ out defaultTenant ) )
230
236
{
231
- account . SetOrAppendProperty ( AzureAccount . Property . Tenants , new [ ] { newTenant . Id . ToString ( ) } ) ;
237
+ account . SetOrAppendProperty ( AzureAccount . Property . Tenants , new [ ] { defaultTenant . Id . ToString ( ) } ) ;
232
238
}
233
239
}
234
240
// (tenant is not provided and subscription is present) OR
235
241
// (tenant is not provided and subscription is not provided)
236
242
else
237
243
{
238
- var tenants = ListAccountTenants ( account , environment , password , promptBehavior , promptAction )
239
- . Select ( s => s . Id . ToString ( ) ) . ToList ( ) ;
244
+ tenants = ListAccountTenants ( account , environment , password , promptBehavior , promptAction ) . ToList ( ) ;
240
245
account . SetProperty ( AzureAccount . Property . Tenants , null ) ;
241
246
string accountId = null ;
242
247
IAzureTenant tempTenant = null ;
@@ -250,57 +255,57 @@ public AzureRmProfile Login(
250
255
251
256
try
252
257
{
253
- token = AcquireAccessToken ( account , environment , tenant , password , ShowDialog . Auto , null ) ;
258
+ token = AcquireAccessToken ( account , environment , tenant . Id , password , ShowDialog . Auto , null ) ;
254
259
if ( accountId == null )
255
260
{
256
261
accountId = account . Id ;
257
- account . SetOrAppendProperty ( AzureAccount . Property . Tenants , tenant ) ;
262
+ account . SetOrAppendProperty ( AzureAccount . Property . Tenants , tenant . Id ) ;
258
263
}
259
264
else if ( accountId . Equals ( account . Id , StringComparison . OrdinalIgnoreCase ) )
260
265
{
261
- account . SetOrAppendProperty ( AzureAccount . Property . Tenants , tenant ) ;
266
+ account . SetOrAppendProperty ( AzureAccount . Property . Tenants , tenant . Id ) ;
262
267
}
263
268
else
264
269
{ // if account ID is different from the first tenant account id we need to ignore current tenant
265
270
WriteWarningMessage ( string . Format (
266
271
ProfileMessages . AccountIdMismatch ,
267
272
account . Id ,
268
- tenant ,
273
+ tenant . Id ,
269
274
accountId ) ) ;
270
275
account . Id = accountId ;
271
276
token = null ;
272
277
}
273
278
}
274
279
catch ( Exception e )
275
280
{
276
- WriteWarningMessage ( string . Format ( ProfileMessages . UnableToAqcuireToken , tenant , e . Message ) ) ;
277
- WriteDebugMessage ( string . Format ( ProfileMessages . UnableToAqcuireToken , tenant , e . ToString ( ) ) ) ;
281
+ WriteWarningMessage ( string . Format ( ProfileMessages . UnableToAqcuireToken , tenant . Id , e . Message ) ) ;
282
+ WriteDebugMessage ( string . Format ( ProfileMessages . UnableToAqcuireToken , tenant . Id , e . ToString ( ) ) ) ;
278
283
}
279
284
280
285
if ( token != null &&
281
- newTenant == null &&
286
+ defaultTenant == null &&
282
287
TryGetTenantSubscription ( token , account , environment , subscriptionId , subscriptionName , false , out tempSubscription , out tempTenant ) )
283
288
{
284
289
// If no subscription found for the given token/tenant,discard tempTenant value.
285
290
// Continue to look for matched subscripitons until one subscription retrived by its home tenant is found.
286
291
if ( tempSubscription != null )
287
292
{
288
- newSubscription = tempSubscription ;
293
+ defaultSubscription = tempSubscription ;
289
294
if ( tempSubscription . GetTenant ( ) == tempSubscription . GetHomeTenant ( ) )
290
295
{
291
- newTenant = tempTenant ;
296
+ defaultTenant = tempTenant ;
292
297
}
293
298
}
294
299
}
295
300
}
296
- newSubscription = newSubscription ?? tempSubscription ;
297
- newTenant = newTenant ??
298
- ( newSubscription != null ? new AzureTenant ( ) { Id = newSubscription . GetTenant ( ) } : tempTenant ) ;
301
+ defaultSubscription = defaultSubscription ?? tempSubscription ;
302
+ defaultTenant = defaultTenant ??
303
+ ( defaultSubscription != null ? new AzureTenant ( ) { Id = defaultSubscription . GetTenant ( ) } : tempTenant ) ;
299
304
}
300
305
}
301
306
302
307
shouldPopulateContextList &= _profile . DefaultContext ? . Account == null ;
303
- if ( newSubscription == null )
308
+ if ( defaultSubscription == null )
304
309
{
305
310
if ( subscriptionId != null )
306
311
{
@@ -311,25 +316,27 @@ public AzureRmProfile Login(
311
316
throw new PSInvalidOperationException ( String . Format ( ResourceMessages . SubscriptionNameNotFound , account . Id , subscriptionName ) + " " + ProfileMessages . SubscriptionNotFouldPleaseCheckConfig ) ;
312
317
}
313
318
314
- var newContext = new AzureContext ( account , environment , newTenant ) ;
315
- if ( ! _profile . TrySetDefaultContext ( name , newContext ) )
319
+ defaultTenant = GetDetailedTenantFromQueryHistory ( tenants , defaultTenant . Id ) ?? defaultTenant ;
320
+ var defaultContext = new AzureContext ( account , environment , defaultTenant ) ;
321
+ if ( ! _profile . TrySetDefaultContext ( name , defaultContext ) )
316
322
{
317
- WriteWarningMessage ( string . Format ( ProfileMessages . CannotSetDefaultContext , newContext . ToString ( ) ) ) ;
323
+ WriteWarningMessage ( string . Format ( ProfileMessages . CannotSetDefaultContext , defaultContext . ToString ( ) ) ) ;
318
324
}
319
325
}
320
326
else
321
327
{
322
- var newContext = new AzureContext ( newSubscription , account , environment , newTenant ) ;
323
- if ( ! _profile . TrySetDefaultContext ( name , newContext ) )
328
+ defaultTenant = GetDetailedTenantFromQueryHistory ( tenants , defaultTenant . Id ) ?? defaultTenant ; ;
329
+ var defaultContext = new AzureContext ( defaultSubscription , account , environment , defaultTenant ) ;
330
+ if ( ! _profile . TrySetDefaultContext ( name , defaultContext ) )
324
331
{
325
- WriteWarningMessage ( string . Format ( ProfileMessages . CannotSetDefaultContext , newContext . ToString ( ) ) ) ;
332
+ WriteWarningMessage ( string . Format ( ProfileMessages . CannotSetDefaultContext , defaultContext . ToString ( ) ) ) ;
326
333
}
327
334
328
- if ( ! skipValidation && ! newSubscription . State . Equals ( "Enabled" , StringComparison . OrdinalIgnoreCase ) )
335
+ if ( ! skipValidation && ! defaultSubscription . State . Equals ( "Enabled" , StringComparison . OrdinalIgnoreCase ) )
329
336
{
330
337
WriteWarningMessage ( string . Format (
331
338
ProfileMessages . SelectedSubscriptionNotActive ,
332
- newSubscription . State ) ) ;
339
+ defaultSubscription . State ) ) ;
333
340
}
334
341
}
335
342
@@ -341,7 +348,7 @@ public AzureRmProfile Login(
341
348
342
349
foreach ( var subscription in subscriptions )
343
350
{
344
- IAzureTenant tempTenant = new AzureTenant ( )
351
+ IAzureTenant tempTenant = GetDetailedTenantFromQueryHistory ( tenants , subscription . GetProperty ( AzureSubscription . Property . Tenants ) ) ?? new AzureTenant ( )
345
352
{
346
353
Id = subscription . GetProperty ( AzureSubscription . Property . Tenants )
347
354
} ;
0 commit comments