Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Merge main into live #2783

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions docs/database/includes/azure-postgresql-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,16 @@ builder.AddNpgsqlDataSource(
"postgresdb",
configureDataSourceBuilder: (dataSourceBuilder) =>
{
if (!string.IsNullOrEmpty(dataSourceBuilder.ConnectionStringBuilder.Password))
{
return;
}

dataSourceBuilder.UsePeriodicPasswordProvider(async (_, ct) =>
if (string.IsNullOrEmpty(dataSourceBuilder.ConnectionStringBuilder.Password))
{
var credentials = new DefaultAzureCredential();
var token = await credentials.GetTokenAsync(
new TokenRequestContext([
"https://ossrdbms-aad.database.windows.net/.default"
]), ct);

return token.Token;
},
TimeSpan.FromHours(24),
TimeSpan.FromSeconds(10));
var tokenRequest = new TokenRequestContext(["https://ossrdbms-aad.database.windows.net/.default"]);

dataSourceBuilder.UsePasswordProvider(
passwordProvider: _ => credentials.GetToken(tokenRequest).Token,
passwordProviderAsync: async (_, ct) => (await credentials.GetTokenAsync(tokenRequest, ct)).Token);
}
});
```

The preceding code snippet demonstrates how to use the <xref:Azure.Identity.DefaultAzureCredential> class from the <xref:Azure.Identity> package to authenticate with [Microsoft Entra ID](/azure/postgresql/flexible-server/concepts-azure-ad-authentication) and retrieve a token to connect to the PostgreSQL database. The [UsePeriodicPasswordProvider](https://www.npgsql.org/doc/api/Npgsql.NpgsqlDataSourceBuilder.html#Npgsql_NpgsqlDataSourceBuilder_UsePeriodicPasswordProvider_System_Func_Npgsql_NpgsqlConnectionStringBuilder_System_Threading_CancellationToken_System_Threading_Tasks_ValueTask_System_String___System_TimeSpan_System_TimeSpan_) method is used to provide the token to the connection string builder.
The preceding code snippet demonstrates how to use the <xref:Azure.Identity.DefaultAzureCredential> class from the <xref:Azure.Identity> package to authenticate with [Microsoft Entra ID](/azure/postgresql/flexible-server/concepts-azure-ad-authentication) and retrieve a token to connect to the PostgreSQL database. The [UsePasswordProvider](https://www.npgsql.org/doc/api/Npgsql.NpgsqlDataSourceBuilder.html#Npgsql_NpgsqlDataSourceBuilder_UsePasswordProvider_System_Func_Npgsql_NpgsqlConnectionStringBuilder_System_String__System_Func_Npgsql_NpgsqlConnectionStringBuilder_System_Threading_CancellationToken_System_Threading_Tasks_ValueTask_System_String___) method is used to provide the token to the data source builder.
62 changes: 39 additions & 23 deletions docs/database/includes/azure-postgresql-ef-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,47 @@ dotnet add package Azure.Identity

---

The PostgreSQL connection can be consumed using the client integration and <xref:Azure.Identity>:
The PostgreSQL connection can be consumed using the client integration and <xref:Azure.Identity>.

The following code snippets demonstrate how to use the <xref:Azure.Identity.DefaultAzureCredential> class from the <xref:Azure.Identity> package to authenticate with [Microsoft Entra ID](/azure/postgresql/flexible-server/concepts-azure-ad-authentication) and retrieve a token to connect to the PostgreSQL database. The [UsePasswordProvider](https://www.npgsql.org/doc/api/Npgsql.NpgsqlDataSourceBuilder.html#Npgsql_NpgsqlDataSourceBuilder_UsePasswordProvider_System_Func_Npgsql_NpgsqlConnectionStringBuilder_System_String__System_Func_Npgsql_NpgsqlConnectionStringBuilder_System_Threading_CancellationToken_System_Threading_Tasks_ValueTask_System_String___) method is used to provide the token to the data source builder.

### EF Core version 8

```csharp
builder.AddNpgsqlDbContext<YourDbContext>(
"postgresdb",
configureDataSourceBuilder: (dataSourceBuilder) =>
var dsBuilder = new NpgsqlDataSourceBuilder(builder.Configuration.GetConnectionString("postgresdb"));
if (string.IsNullOrEmpty(dsBuilder.ConnectionStringBuilder.Password))
{
if (!string.IsNullOrEmpty(dataSourceBuilder.ConnectionStringBuilder.Password))
{
return;
}

dataSourceBuilder.UsePeriodicPasswordProvider(async (_, ct) =>
{
var credentials = new DefaultAzureCredential();
var token = await credentials.GetTokenAsync(
new TokenRequestContext([
"https://ossrdbms-aad.database.windows.net/.default"
]), ct);

return token.Token;
},
TimeSpan.FromHours(24),
TimeSpan.FromSeconds(10));
});
var credentials = new DefaultAzureCredential();
var tokenRequest = new TokenRequestContext(["https://ossrdbms-aad.database.windows.net/.default"]);

dsBuilder.UsePasswordProvider(
passwordProvider: _ => credentials.GetToken(tokenRequest).Token,
passwordProviderAsync: async (_, ct) => (await credentials.GetTokenAsync(tokenRequest, ct)).Token);
}

builder.AddNpgsqlDbContext<MyDb1Context>(
"postgresdb",
configureDbContextOptions: (options) => options.UseNpgsql(dsBuilder.Build()));
```

The preceding code snippet demonstrates how to use the <xref:Azure.Identity.DefaultAzureCredential> class from the <xref:Azure.Identity> package to authenticate with [Microsoft Entra ID](/azure/postgresql/flexible-server/concepts-azure-ad-authentication) and retrieve a token to connect to the PostgreSQL database. The [UsePeriodicPasswordProvider](https://www.npgsql.org/doc/api/Npgsql.NpgsqlDataSourceBuilder.html#Npgsql_NpgsqlDataSourceBuilder_UsePeriodicPasswordProvider_System_Func_Npgsql_NpgsqlConnectionStringBuilder_System_Threading_CancellationToken_System_Threading_Tasks_ValueTask_System_String___System_TimeSpan_System_TimeSpan_) method is used to provide the token to the connection string builder.
### EF Core version 9+

With EF Core version 9, you can use the `ConfigureDataSource` method to configure the `NpgsqlDataSourceBuilder` that's used by the integration instead of building one outside of the integration and passing it in.

```csharp
builder.AddNpgsqlDbContext<MyDb1Context>(
"postgresdb",
configureDbContextOptions: (options) => options.UseNpgsql(npgsqlOptions =>
npgsqlOptions.ConfigureDataSource(dsBuilder =>
{
if (string.IsNullOrEmpty(dsBuilder.ConnectionStringBuilder.Password))
{
var credentials = new DefaultAzureCredential();
var tokenRequest = new TokenRequestContext(["https://ossrdbms-aad.database.windows.net/.default"]);

dsBuilder.UsePasswordProvider(
passwordProvider: _ => credentials.GetToken(tokenRequest).Token,
passwordProviderAsync: async (_, ct) => (await credentials.GetTokenAsync(tokenRequest, ct)).Token);
}
})));
```
Loading