-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
51 lines (43 loc) · 1.72 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using Azure.Core;
using Azure.Identity;
using Npgsql;
namespace Ad_Poc;
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//--------- Configuração do EntraId / Ad no PostgreSQL
var connectionString = builder.Configuration["postgre:database"]!;
var azurePostgreTokenScope = builder.Configuration["postgre:tokenScope"]!;
var postgreUserName = await PostgreUserService.GetPostgreSqlAuthUserName(builder.Configuration);
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(connectionString)
{
Username = postgreUserName
};
builder.Services.AddNpgsqlDataSource(connectionStringBuilder.ToString(), dataSourceBuilder =>
{
dataSourceBuilder.UsePeriodicPasswordProvider(async (_, cancellationToken) =>
{
var azureCredential = new DefaultAzureCredential();
var tokenScope = new TokenRequestContext([azurePostgreTokenScope]);
var tokenResponse = await azureCredential.GetTokenAsync(tokenScope, cancellationToken);
return tokenResponse.Token;
}, TimeSpan.FromHours(3), TimeSpan.FromSeconds(5));
});
//--------- Fim da Configuração do PostgreSQL
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}