22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
44using System ;
5+ using System . Globalization ;
56using System . Net . Http ;
67using System . Net . Http . Headers ;
78using System . Threading ;
@@ -12,11 +13,12 @@ namespace NuGet.Services.AzureManagement
1213{
1314 public class AzureManagementAPIWrapper : IAzureManagementAPIWrapper
1415 {
15- private const string Authority = "https://login.microsoftonline.com/microsoft.onmicrosoft.com " ;
16+ private const string AuthorityTemplate = "https://login.microsoftonline.com/{0} " ;
1617 private const string Resource = "https://management.core.windows.net/" ;
1718 private const int RenewTokenPriorToExpirationMinutes = 5 ;
1819
1920 private readonly ClientCredential _clientCredential ;
21+ private readonly string _authority ;
2022
2123 private string _accessToken ;
2224 private DateTimeOffset _tokenExpirationTime ;
@@ -38,7 +40,14 @@ public AzureManagementAPIWrapper(IAzureManagementAPIWrapperConfiguration configu
3840 throw new ArgumentException ( nameof ( configuration . ClientSecret ) ) ;
3941 }
4042
43+ if ( string . IsNullOrEmpty ( configuration . AadTenant ) )
44+ {
45+ throw new ArgumentException ( nameof ( configuration . AadTenant ) ) ;
46+ }
47+
48+
4149 _clientCredential = new ClientCredential ( configuration . ClientId , configuration . ClientSecret ) ;
50+ _authority = string . Format ( CultureInfo . InvariantCulture , AuthorityTemplate , configuration . AadTenant ) ;
4251 }
4352
4453 public async Task RebootCloudServiceRoleInstanceAsync (
@@ -208,13 +217,13 @@ private async Task<AuthenticationResult> GetAccessToken()
208217 {
209218 try
210219 {
211- var context = new AuthenticationContext ( Authority , validateAuthority : false ) ;
220+ var context = new AuthenticationContext ( _authority , validateAuthority : false ) ;
212221 AuthenticationResult authenticationResult = await context . AcquireTokenAsync ( Resource , _clientCredential ) ;
213222 return authenticationResult ;
214223 }
215224 catch ( AdalException adalException )
216225 {
217- throw new AzureManagementException ( $ "Failed to create token. Client id: { _clientCredential . ClientId } ", adalException ) ;
226+ throw new AzureManagementException ( $ "Failed to create token. Client id: { _clientCredential . ClientId } , Authority: { _authority } ", adalException ) ;
218227 }
219228 }
220229 }
0 commit comments