-
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.
Add EF Core interceptor to use AAD auth and token caching
- Loading branch information
Showing
6 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/AzureIdentityLivestream.Web/Services/Sql/AzureAdAuthenticationDbConnectionInterceptor.cs
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Data.Common; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Data.SqlClient; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
|
||
namespace AzureIdentityLivestream.Web.Services.Sql | ||
{ | ||
public class AzureAdAuthenticationDbConnectionInterceptor : DbConnectionInterceptor | ||
{ | ||
private readonly IAzureSqlTokenProvider _azureSqlTokenProvider; | ||
|
||
public AzureAdAuthenticationDbConnectionInterceptor(IAzureSqlTokenProvider azureSqlTokenProvider) | ||
{ | ||
_azureSqlTokenProvider = azureSqlTokenProvider; | ||
} | ||
|
||
public override InterceptionResult ConnectionOpening(DbConnection connection, ConnectionEventData eventData, InterceptionResult result) | ||
{ | ||
var sqlConnection = (SqlConnection)connection; | ||
if (ConnectionNeedsAccessToken(sqlConnection)) | ||
{ | ||
var (token, _) = _azureSqlTokenProvider.GetAccessToken(); | ||
sqlConnection.AccessToken = token; | ||
} | ||
|
||
return base.ConnectionOpening(connection, eventData, result); | ||
} | ||
|
||
public override async ValueTask<InterceptionResult> ConnectionOpeningAsync(DbConnection connection, ConnectionEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) | ||
{ | ||
var sqlConnection = (SqlConnection)connection; | ||
if (ConnectionNeedsAccessToken(sqlConnection)) | ||
{ | ||
var (token, _) = await _azureSqlTokenProvider.GetAccessTokenAsync(cancellationToken); | ||
sqlConnection.AccessToken = token; | ||
} | ||
|
||
return await base.ConnectionOpeningAsync(connection, eventData, result, cancellationToken); | ||
} | ||
|
||
private static bool ConnectionNeedsAccessToken(SqlConnection connection) | ||
{ | ||
var connectionStringBuilder = new SqlConnectionStringBuilder(connection.ConnectionString); | ||
|
||
return connectionStringBuilder.DataSource.Contains("database.windows.net", StringComparison.OrdinalIgnoreCase) && | ||
string.IsNullOrEmpty(connectionStringBuilder.UserID); | ||
} | ||
} | ||
} |
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
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