-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
From Microsoft.Azure.Services.AppAuthentication
- Loading branch information
Showing
3 changed files
with
26 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,27 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Services.AppAuthentication; | ||
using System; | ||
using System.Threading.Tasks; | ||
using Azure.Core; | ||
using Azure.Identity; | ||
|
||
namespace SqlBulkSyncFunction.Services | ||
namespace SqlBulkSyncFunction.Services; | ||
|
||
public record AzureSqlTokenService(DefaultAzureCredential DefaultAzureCredential) : IAzureSqlTokenService | ||
{ | ||
// ReSharper disable once UnusedMember.Global | ||
public record AzureSqlTokenService(AzureServiceTokenProvider AzureServiceTokenProvider) : IAzureSqlTokenService | ||
{ | ||
private const string AzureSqlResourceId = "https://database.windows.net/"; | ||
|
||
public async Task<string> GetAccessToken(string tenantId) | ||
=> await AzureServiceTokenProvider.GetAccessTokenAsync(AzureSqlResourceId, string.IsNullOrWhiteSpace(tenantId) ? null : tenantId); | ||
} | ||
private const string AzureSqlResourceId = "https://database.windows.net/"; | ||
private static AccessToken? AccessToken = default; | ||
|
||
|
||
public async Task<string> GetAccessToken(string tenantId) | ||
=> ( | ||
AccessToken is { } validToken && validToken.ExpiresOn < DateTimeOffset.UtcNow.AddMinutes(5) | ||
? validToken | ||
: (AccessToken = await DefaultAzureCredential.GetTokenAsync( | ||
new TokenRequestContext( | ||
new[] { AzureSqlResourceId }, | ||
tenantId: string.IsNullOrWhiteSpace(tenantId) ? null : tenantId | ||
) | ||
) | ||
).Value | ||
).Token; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters