EF Core & Azure SQL with Managed Identity (no `IDBAuthTokenService`)

azure-active-directory azure-sql-database c# ef-core-2.2 entity-framework-core

Accepted Answer

You may employ Microsoft.Azure.Services.AppAuthentication.

enter image description here



22 to 22 in

services.AddSingleton<AzureServiceTokenProvider>(new AzureServiceTokenProvider());
services.AddDbContext<SqlDBContext>();

You have a DbContext.cs:

private AzureServiceTokenProvider azureServiceTokenProvider;
public SqlDBContext(DbContextOptions<SqlDBContext> options, AzureServiceTokenProvider azureServiceTokenProvider) : base(options)
{
    this.azureServiceTokenProvider = azureServiceTokenProvider;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    SqlConnection connection = new SqlConnection();
    connection.ConnectionString = "Data Source=tcp:jackdemo.database.windows.net,1433; Initial Catalog=jackdemo; "; 
    connection.AccessToken = azureServiceTokenProvider.GetAccessTokenAsync("https://database.windows.net/").Result;
    optionsBuilder.UseSqlServer(connection);
}

0
10/23/2019 7:28:26 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow