@@ -41,12 +41,13 @@ public AuthenticationServiceFactory(IPathUtility pathUtility, IAuthenticationCac
4141 /// <param name="clientId">Client Id</param>
4242 /// <param name="certificateName">Certificate name</param>
4343 /// <param name="certificateThumbPrint">Certificate thumb-print</param>
44+ /// <param name="environment">The national cloud environment. Either 'Global', 'US_GOV', 'US_GOV_DOD' or 'China'</param>
4445 /// <param name="cancellationToken">Cancellation token</param>
4546 /// <returns>Returns a login service instance.</returns>
4647 /// <exception cref="InvalidOperationException">When an unsupported authentication strategy is provided.</exception>
47- public virtual async Task < LoginServiceBase > GetAuthenticationServiceAsync ( AuthenticationStrategy strategy , string ? tenantId , string ? clientId , string ? certificateName , string ? certificateThumbPrint , CancellationToken cancellationToken = default )
48+ public virtual async Task < LoginServiceBase > GetAuthenticationServiceAsync ( AuthenticationStrategy strategy , string ? tenantId , string ? clientId , string ? certificateName , string ? certificateThumbPrint , CloudEnvironment environment , CancellationToken cancellationToken = default )
4849 {
49- var credential = await GetTokenCredentialAsync ( strategy , tenantId , clientId , certificateName , certificateThumbPrint , cancellationToken ) ;
50+ var credential = await GetTokenCredentialAsync ( strategy , tenantId , clientId , certificateName , certificateThumbPrint , environment , cancellationToken ) ;
5051 if ( strategy == AuthenticationStrategy . DeviceCode && credential is DeviceCodeCredential deviceCred )
5152 {
5253 return new InteractiveLoginService < DeviceCodeCredential > ( deviceCred , pathUtility ) ;
@@ -81,35 +82,33 @@ public virtual async Task<LoginServiceBase> GetAuthenticationServiceAsync(Authen
8182 /// <param name="clientId">Client Id</param>
8283 /// <param name="certificateName">Certificate name</param>
8384 /// <param name="certificateThumbPrint">Certificate thumb-print</param>
85+ /// <param name="environment">The cloud environment. <see cref="CloudEnvironment"/></param>
8486 /// <param name="cancellationToken">Cancellation token.</param>
8587 /// <returns>A token credential instance.</returns>
8688 /// <exception cref="InvalidOperationException">When an unsupported authentication strategy is provided.</exception>
87- public virtual async Task < TokenCredential > GetTokenCredentialAsync ( AuthenticationStrategy strategy , string ? tenantId , string ? clientId , string ? certificateName , string ? certificateThumbPrint , CancellationToken cancellationToken = default )
89+ /// <exception cref="ArgumentNullException">When a null url is provided for the authority host.</exception>
90+ public virtual async Task < TokenCredential > GetTokenCredentialAsync ( AuthenticationStrategy strategy , string ? tenantId , string ? clientId , string ? certificateName , string ? certificateThumbPrint , CloudEnvironment environment , CancellationToken cancellationToken = default )
8891 {
89- switch ( strategy )
92+ var authorityHost = environment . Authority ( ) ;
93+ return strategy switch
9094 {
91- case AuthenticationStrategy . DeviceCode :
92- return await GetDeviceCodeCredentialAsync ( tenantId , clientId , cancellationToken ) ;
93- case AuthenticationStrategy . InteractiveBrowser :
94- return await GetInteractiveBrowserCredentialAsync ( tenantId , clientId , cancellationToken ) ;
95- case AuthenticationStrategy . ClientCertificate :
96- return GetClientCertificateCredential ( tenantId , clientId , certificateName , certificateThumbPrint ) ;
97- case AuthenticationStrategy . Environment :
98- return new EnvironmentCredential ( tenantId , clientId ) ;
99- case AuthenticationStrategy . ManagedIdentity :
100- return new ManagedIdentityCredential ( clientId ) ;
101- default :
102- throw new InvalidOperationException ( $ "The authentication strategy { strategy } is not supported") ;
103- }
95+ AuthenticationStrategy . DeviceCode => await GetDeviceCodeCredentialAsync ( tenantId , clientId , authorityHost , cancellationToken ) ,
96+ AuthenticationStrategy . InteractiveBrowser => await GetInteractiveBrowserCredentialAsync ( tenantId , clientId , authorityHost , cancellationToken ) ,
97+ AuthenticationStrategy . ClientCertificate => GetClientCertificateCredential ( tenantId , clientId , certificateName , certificateThumbPrint , authorityHost ) ,
98+ AuthenticationStrategy . Environment => new EnvironmentCredential ( tenantId , clientId , new TokenCredentialOptions { AuthorityHost = authorityHost } ) ,
99+ AuthenticationStrategy . ManagedIdentity => new ManagedIdentityCredential ( clientId , new TokenCredentialOptions { AuthorityHost = authorityHost } ) ,
100+ _ => throw new InvalidOperationException ( $ "The authentication strategy { strategy } is not supported") ,
101+ } ;
104102 }
105103
106- private async Task < DeviceCodeCredential > GetDeviceCodeCredentialAsync ( string ? tenantId , string ? clientId , CancellationToken cancellationToken = default )
104+ private async Task < DeviceCodeCredential > GetDeviceCodeCredentialAsync ( string ? tenantId , string ? clientId , Uri authorityHost , CancellationToken cancellationToken = default )
107105 {
108106 DeviceCodeCredentialOptions credOptions = new ( )
109107 {
110108 ClientId = clientId ?? Constants . DefaultAppId ,
111109 TenantId = tenantId ?? Constants . DefaultTenant ,
112110 DisableAutomaticAuthentication = true ,
111+ AuthorityHost = authorityHost
113112 } ;
114113
115114 TokenCachePersistenceOptions tokenCacheOptions = new ( ) { Name = Constants . TokenCacheName } ;
@@ -119,13 +118,14 @@ private async Task<DeviceCodeCredential> GetDeviceCodeCredentialAsync(string? te
119118 return new DeviceCodeCredential ( credOptions ) ;
120119 }
121120
122- private async Task < InteractiveBrowserCredential > GetInteractiveBrowserCredentialAsync ( string ? tenantId , string ? clientId , CancellationToken cancellationToken = default )
121+ private async Task < InteractiveBrowserCredential > GetInteractiveBrowserCredentialAsync ( string ? tenantId , string ? clientId , Uri authorityHost , CancellationToken cancellationToken = default )
123122 {
124123 InteractiveBrowserCredentialOptions credOptions = new ( )
125124 {
126125 ClientId = clientId ?? Constants . DefaultAppId ,
127126 TenantId = tenantId ?? Constants . DefaultTenant ,
128127 DisableAutomaticAuthentication = true ,
128+ AuthorityHost = authorityHost
129129 } ;
130130
131131 TokenCachePersistenceOptions tokenCacheOptions = new ( ) { Name = Constants . TokenCacheName } ;
@@ -135,8 +135,8 @@ private async Task<InteractiveBrowserCredential> GetInteractiveBrowserCredential
135135 return new InteractiveBrowserCredential ( credOptions ) ;
136136 }
137137
138- private ClientCertificateCredential GetClientCertificateCredential ( string ? tenantId , string ? clientId , string ? certificateName , string ? certificateThumbPrint )
138+ private ClientCertificateCredential GetClientCertificateCredential ( string ? tenantId , string ? clientId , string ? certificateName , string ? certificateThumbPrint , Uri authorityHost )
139139 {
140- return ClientCertificateCredentialFactory . GetClientCertificateCredential ( tenantId ?? Constants . DefaultTenant , clientId ?? Constants . DefaultAppId , certificateName , certificateThumbPrint ) ;
140+ return ClientCertificateCredentialFactory . GetClientCertificateCredential ( tenantId ?? Constants . DefaultTenant , clientId ?? Constants . DefaultAppId , certificateName , certificateThumbPrint , authorityHost ) ;
141141 }
142142}
0 commit comments