1111// See the License for the specific language governing permissions and
1212// limitations under the License.
1313// ----------------------------------------------------------------------------------
14+
1415using Microsoft . Azure . Commands . Common . Authentication ;
1516using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
1617using Microsoft . Azure . Commands . Common . Authentication . Abstractions . Interfaces ;
2223using Microsoft . Azure . Commands . Profile . Utilities ;
2324using Microsoft . Rest . Azure ;
2425using Microsoft . WindowsAzure . Commands . Common ;
25-
2626using System ;
2727using System . Collections . Generic ;
2828using System . Linq ;
2929using System . Management . Automation ;
3030using System . Security ;
31-
3231using AuthenticationMessages = Microsoft . Azure . Commands . Common . Authentication . Properties . Resources ;
3332using ProfileMessages = Microsoft . Azure . Commands . Profile . Properties . Resources ;
3433using ResourceMessages = Microsoft . Azure . Commands . ResourceManager . Common . Properties . Resources ;
@@ -52,7 +51,7 @@ private IAzureContext DefaultContext
5251 {
5352 get
5453 {
55- if ( _profile == null || _profile . DefaultContext == null || _profile . DefaultContext . Account == null )
54+ if ( _profile == null || _profile . DefaultContext == null || _profile . DefaultContext . Account == null )
5655 {
5756 throw new PSInvalidOperationException ( ResourceMessages . RunConnectAccount ) ;
5857 }
@@ -130,21 +129,22 @@ public AzureRmProfile Login(
130129 bool skipValidation ,
131130 IOpenIDConfiguration openIDConfigDoc ,
132131 Action < string > promptAction ,
132+ string claimsChallenge = null ,
133133 string name = null ,
134134 bool shouldPopulateContextList = true ,
135135 int maxContextPopulation = Profile . ConnectAzureRmAccountCommand . DefaultMaxContextPopulation ,
136136 string authScope = null ,
137137 bool IsInteractiveContextSelectionEnabled = true )
138138 {
139-
139+
140140 WriteInteractiveInformationMessage ( $ "{ PSStyle . ForegroundColor . BrightYellow } { Resources . PleaseSelectAccount } { PSStyle . Reset } { System . Environment . NewLine } ") ;
141141
142142 IAzureSubscription defaultSubscription = null ;
143143 IAzureTenant defaultTenant = null ;
144144 List < AzureSubscription > subscriptions = new List < AzureSubscription > ( ) ;
145145 List < AzureSubscription > tempSubscriptions = null ;
146146 string tenantName = null ;
147-
147+
148148 bool selectSubscriptionFromList = AzureAccount . AccountType . User . Equals ( account . Type ) &&
149149 IsInteractiveContextSelectionEnabled &&
150150 string . IsNullOrEmpty ( subscriptionId ) &&
@@ -161,9 +161,9 @@ public AzureRmProfile Login(
161161 SubscritpionClientCandidates . Reset ( ) ;
162162
163163 bool needDataPlanAuthFirst = ! string . IsNullOrEmpty ( authScope ) ;
164- if ( needDataPlanAuthFirst )
164+ if ( needDataPlanAuthFirst )
165165 {
166- var token = AcquireAccessToken ( account , environment , tenantIdOrName , password , promptBehavior , promptAction , authScope ) ;
166+ var token = AcquireAccessToken ( account , environment , tenantIdOrName , password , promptBehavior , promptAction , claimsChallenge , authScope ) ;
167167 promptBehavior = ShowDialog . Never ;
168168 }
169169
@@ -202,7 +202,8 @@ public AzureRmProfile Login(
202202 tenantIdOrName ,
203203 password ,
204204 promptBehavior ,
205- promptAction ) ;
205+ promptAction ,
206+ claimsChallenge ) ;
206207
207208 if ( ! Guid . TryParse ( tenantIdOrName , out Guid _ ) )
208209 {
@@ -229,7 +230,7 @@ public AzureRmProfile Login(
229230 }
230231 }
231232 }
232- catch ( Exception e )
233+ catch ( Exception e )
233234 {
234235 string baseMessage = string . Format ( ProfileMessages . TenantDomainNotFound , tenantIdOrName ) ;
235236 var typeMessageMap = new Dictionary < string , string >
@@ -293,7 +294,7 @@ public AzureRmProfile Login(
293294
294295 try
295296 {
296- token = AcquireAccessToken ( account , environment , tenant . Id , password , ShowDialog . Auto , null ) ;
297+ token = AcquireAccessToken ( account , environment , tenant . Id , password , ShowDialog . Auto , null , claimsChallenge ) ;
297298 if ( accountId == null )
298299 {
299300 accountId = account . Id ;
@@ -314,7 +315,7 @@ public AzureRmProfile Login(
314315 token = null ;
315316 }
316317 }
317- catch ( Exception e )
318+ catch ( Exception e )
318319 {
319320 WriteWarningMessage ( string . Format ( ProfileMessages . UnableToAqcuireToken , tenant . Id , e . Message ) ) ;
320321 WriteDebugMessage ( string . Format ( ProfileMessages . UnableToAqcuireToken , tenant . Id , e . ToString ( ) ) ) ;
@@ -334,7 +335,7 @@ public AzureRmProfile Login(
334335 defaultTenant = tempTenant ;
335336 }
336337 }
337- if ( tempSubscription != null )
338+ if ( tempSubscription != null )
338339 {
339340 subscriptions . AddRange ( tempSubscriptions ) ;
340341 }
@@ -397,7 +398,7 @@ public AzureRmProfile Login(
397398 {
398399 var defaultContext = _profile . DefaultContext ;
399400 var populatedSubscriptions = ( maxContextPopulation < 0 || selectSubscriptionFromList ) ? ListSubscriptions ( tenantIdOrName ) : ListSubscriptions ( tenantIdOrName ) . Take ( maxContextPopulation ) ;
400-
401+
401402 foreach ( var subscription in populatedSubscriptions )
402403 {
403404 IAzureTenant tempTenant = InteractiveSubscriptionSelectionHelper . GetDetailedTenantFromQueryHistory ( _queriedTenants , subscription . GetProperty ( AzureSubscription . Property . Tenants ) ) ?? new AzureTenant ( )
@@ -449,7 +450,7 @@ public IAzureContext SetCurrentContext(string subscriptionNameOrId, string tenan
449450 }
450451
451452 var tenantFromSubscription = subscription . GetTenant ( ) ;
452- tenant = string . IsNullOrWhiteSpace ( tenantId ) ? ( string . IsNullOrEmpty ( tenantFromSubscription ) ? context . Tenant : CreateTenant ( tenantFromSubscription ) ) : CreateTenant ( tenantId ) ;
453+ tenant = string . IsNullOrWhiteSpace ( tenantId ) ? ( string . IsNullOrEmpty ( tenantFromSubscription ) ? context . Tenant : CreateTenant ( tenantFromSubscription ) ) : CreateTenant ( tenantId ) ;
453454 }
454455 else if ( ! string . IsNullOrWhiteSpace ( tenantId ) )
455456 {
@@ -536,14 +537,14 @@ public bool TryGetSubscriptionListByName(string tenantId, string subscriptionNam
536537 HashSet < Guid > existedSubscriptionIds = new HashSet < Guid > ( ) ;
537538
538539 // Consider subscription in Home tenant first, exclude duplicate subscriptions by id.
539- foreach ( IAzureSubscription subscription in subscriptions )
540+ foreach ( IAzureSubscription subscription in subscriptions )
540541 {
541- if ( subscription is PSAzureSubscription && subscription . GetTenant ( ) != null
542+ if ( subscription is PSAzureSubscription && subscription . GetTenant ( ) != null
542543 && subscription . GetHomeTenant ( ) . Equals ( subscription . GetTenant ( ) ) && existedSubscriptionIds . Add ( subscription . GetId ( ) ) )
543544 {
544545 subscriptionList . Add ( subscription ) ;
545546 }
546-
547+
547548 }
548549 // Consider other subscriptions.
549550 foreach ( IAzureSubscription subscription in subscriptions )
@@ -679,6 +680,7 @@ private IAccessToken AcquireAccessToken(
679680 SecureString password ,
680681 string promptBehavior ,
681682 Action < string > promptAction ,
683+ string claimsChallenge = null ,
682684 string resourceId = AzureEnvironment . Endpoint . ActiveDirectoryServiceEndpointResourceId )
683685 {
684686 if ( account . Type == AzureAccount . AccountType . AccessToken )
@@ -689,11 +691,13 @@ private IAccessToken AcquireAccessToken(
689691
690692 var optionalParameters = new Dictionary < string , object > ( )
691693 {
692- { AuthenticationFactory . TokenCacheParameterName , _cache } ,
693- { AuthenticationFactory . ResourceIdParameterName , resourceId } ,
694- { AuthenticationFactory . CmdletContextParameterName , CmdletContext }
694+ { AuthenticationFactory . ResourceIdParameterName , resourceId } ,
695+ { AuthenticationFactory . ClaimsChallengeParameterName , claimsChallenge } ,
696+ { AuthenticationFactory . TokenCacheParameterName , _cache } ,
697+ { AuthenticationFactory . CmdletContextParameterName , CmdletContext }
695698 } ;
696699
700+
697701 return AzureSession . Instance . AuthenticationFactory . Authenticate (
698702 account ,
699703 environment ,
@@ -814,7 +818,7 @@ private List<AzureTenant> ListAccountTenants(
814818
815819 result = SubscriptionAndTenantClient ? . ListAccountTenants ( commonTenantToken , environment ) ;
816820 }
817- catch ( Exception e )
821+ catch ( Exception e )
818822 {
819823 WriteWarningMessage ( string . Format ( ProfileMessages . UnableToAqcuireToken , commonTenant , e . Message ) ) ;
820824 WriteDebugMessage ( string . Format ( ProfileMessages . UnableToAqcuireToken , commonTenant , e . ToString ( ) ) ) ;
@@ -861,7 +865,7 @@ private IEnumerable<AzureSubscription> ListAllSubscriptionsForTenant(
861865 {
862866 accessToken = AcquireAccessToken ( account , environment , tenantId , password , promptBehavior , null ) ;
863867 }
864- catch ( Exception e )
868+ catch ( Exception e )
865869 {
866870 WriteWarningMessage ( string . Format ( ProfileMessages . UnableToAqcuireToken , tenantId , e . Message ) ) ;
867871 WriteDebugMessage ( string . Format ( ProfileMessages . UnableToAqcuireToken , tenantId , e . ToString ( ) ) ) ;
@@ -881,7 +885,7 @@ private void WriteWarningMessage(string message)
881885
882886 private void WriteDebugMessage ( string message )
883887 {
884- if ( DebugLog != null )
888+ if ( DebugLog != null )
885889 {
886890 DebugLog ( message ) ;
887891 }
0 commit comments