diff --git a/global.json b/global.json index d3a38df..98a6ea8 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "7.0.101", + "version": "7.0.400", "rollForward": "latestFeature", "allowPrerelease": false } diff --git a/src/SqlBulkSyncFunction/Program.cs b/src/SqlBulkSyncFunction/Program.cs index 6b23ded..064f2b9 100644 --- a/src/SqlBulkSyncFunction/Program.cs +++ b/src/SqlBulkSyncFunction/Program.cs @@ -13,7 +13,7 @@ (settings, configuration) => configuration.GetSection(nameof(SyncJobsConfig)).Bind(settings)); configure - .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton(); diff --git a/src/SqlBulkSyncFunction/Services/AzureSqlTokenService.cs b/src/SqlBulkSyncFunction/Services/AzureSqlTokenService.cs index b3d411f..0bdd46e 100644 --- a/src/SqlBulkSyncFunction/Services/AzureSqlTokenService.cs +++ b/src/SqlBulkSyncFunction/Services/AzureSqlTokenService.cs @@ -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 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 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; } + diff --git a/src/SqlBulkSyncFunction/SqlBulkSyncFunction.csproj b/src/SqlBulkSyncFunction/SqlBulkSyncFunction.csproj index dc67469..e789554 100644 --- a/src/SqlBulkSyncFunction/SqlBulkSyncFunction.csproj +++ b/src/SqlBulkSyncFunction/SqlBulkSyncFunction.csproj @@ -8,14 +8,14 @@ - - - - - - - - + + + + + + + + @@ -49,8 +49,8 @@ - - - + + +