Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit b57d691

Browse files
authored
Add support to any AAD authority when authenticating with Azure Management (#295)
* Add support to any AAD authority when authenticating with Azure Management * Add input check
1 parent aeaeb66 commit b57d691

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

src/NuGet.Services.AzureManagement/AzureManagementAPIWrapper.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Globalization;
56
using System.Net.Http;
67
using System.Net.Http.Headers;
78
using 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
}

src/NuGet.Services.AzureManagement/IAzureManagementAPIWrapperConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ public interface IAzureManagementAPIWrapperConfiguration
99
string ClientId { get; }
1010

1111
string ClientSecret { get; }
12+
13+
string AadTenant { get; }
1214
}
1315
}

0 commit comments

Comments
 (0)