-
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.
Merge pull request #20 from devlead/feature/dependencies
Update dependencies & Migrate to Azure Identity
- Loading branch information
Showing
4 changed files
with
37 additions
and
24 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
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