Skip to content

Commit

Permalink
Migrate to Azure.Identity
Browse files Browse the repository at this point in the history
From Microsoft.Azure.Services.AppAuthentication
  • Loading branch information
devlead committed Aug 29, 2023
1 parent ca0089f commit e84fe35
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/SqlBulkSyncFunction/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(settings, configuration) => configuration.GetSection(nameof(SyncJobsConfig)).Bind(settings));
configure
.AddSingleton<Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProvider>()
.AddSingleton<Azure.Identity.DefaultAzureCredential>()
.AddSingleton<IAzureSqlTokenService, AzureSqlTokenService>()
.AddSingleton<IProcessSyncJobService, ProcessSyncJobService>()
.AddSingleton<ITokenCacheService, TokenCacheService>();
Expand Down
35 changes: 24 additions & 11 deletions src/SqlBulkSyncFunction/Services/AzureSqlTokenService.cs
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;
}

2 changes: 1 addition & 1 deletion src/SqlBulkSyncFunction/SqlBulkSyncFunction.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.14.1" OutputItemType="Analyzer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.19.0" />
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />
<PackageReference Include="Azure.Identity" Version="1.10.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.1" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit e84fe35

Please sign in to comment.