Skip to content

Commit

Permalink
Use native AAD support in Microsoft.Data.SqlClient v3
Browse files Browse the repository at this point in the history
  • Loading branch information
mderriey committed Oct 3, 2021
1 parent 86b0427 commit 6b77274
Show file tree
Hide file tree
Showing 10 changed files with 8 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.1" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="3.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
<PackageReference Include="Azure.Storage.Blobs" Version="12.8.0" />
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.16.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.1" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="3.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.3" />
<PackageReference Include="Scrutor" Version="3.3.0" />
</ItemGroup>

</Project>

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public DapperPersonProvider(SqlConnectionFactory sqlConnectionFactory)

public async Task<IEnumerable<Person>> GetPeople()
{
await using var sqlConnection = await _sqlConnectionFactory.CreateConnection();
await using var sqlConnection = _sqlConnectionFactory.CreateConnection();
return await sqlConnection.QueryAsync<Person>("SELECT [FirstName], [LastName], [Age] FROM [dbo].[Person]");
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,12 @@ namespace AzureIdentityLivestream.Web.Services.Sql
public class SqlConnectionFactory
{
private readonly string _connectionString;
private readonly IAzureSqlTokenProvider _azureSqlTokenProvider;

public SqlConnectionFactory(string connectionString, IAzureSqlTokenProvider azureSqlTokenProvider)
public SqlConnectionFactory(string connectionString)
{
_connectionString = connectionString;
_azureSqlTokenProvider = azureSqlTokenProvider;
}

public async Task<SqlConnection> CreateConnection()
{
var sqlConnection = new SqlConnection(_connectionString);

var sqlConnectionStringBuilder = new SqlConnectionStringBuilder(_connectionString);
if (sqlConnection.DataSource.Contains("database.windows.net", StringComparison.OrdinalIgnoreCase) &&
string.IsNullOrEmpty(sqlConnectionStringBuilder.UserID))
{
var (token, _) = await _azureSqlTokenProvider.GetAccessTokenAsync();
sqlConnection.AccessToken = token;
}

return sqlConnection;
}
public SqlConnection CreateConnection() => new(_connectionString);
}
}
10 changes: 2 additions & 8 deletions src/AzureIdentityLivestream.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,11 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry();

services.AddMemoryCache();
services.AddSingleton<IAzureSqlTokenProvider, AzureIdentityAzureSqlTokenProvider>();
services.Decorate<IAzureSqlTokenProvider, CacheAzureSqlTokenProvider>();
services.AddSingleton<AzureAdAuthenticationDbConnectionInterceptor>();

services.AddDbContext<LivestreamContext>((provider, builder) =>
{
var sqlConnectionString = Configuration.GetValue<string>("SqlConnectionString");
builder
.UseSqlServer(sqlConnectionString, options => options.EnableRetryOnFailure())
.AddInterceptors(provider.GetRequiredService<AzureAdAuthenticationDbConnectionInterceptor>());

builder.UseSqlServer(sqlConnectionString, options => options.EnableRetryOnFailure());
});

services.AddScoped<IPersonProvider, EfCorePersonProvider>();
Expand Down
2 changes: 1 addition & 1 deletion src/AzureIdentityLivestream.Web/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"StorageConnectionString": "https://azureidentitylivestream.blob.core.windows.net",
"SqlConnectionString": "Data Source=tcp:azureidentitylivestream.database.windows.net,1433; Initial Catalog=azure-identity-livestream; Encrypt=True; TrustServerCertificate=False",
"SqlConnectionString": "Data Source=tcp:azureidentitylivestream.database.windows.net,1433; Initial Catalog=azure-identity-livestream; Authentication=Active Directory Default; Encrypt=True; TrustServerCertificate=False",
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down

0 comments on commit 6b77274

Please sign in to comment.