Identity Server EF Core DB Configuration with Azure MSI azure-sql-database entity-framework-core identityserver4


I am trying to configure my Identity Server instance that is hosted as an Azure Web App to connect to my database using the Managed Service Identity (

At the time of this article linked above being written, EF Core did not support Access Tokens for SQL Server connections. It appears this has changed with release 2.2 that is currently in preview.

Currently my configuration store setup looks like this:

.AddConfigurationStore(options =>
                options.ConfigureDbContext = b =>
                        sql => { sql.MigrationsAssembly(migrationsAssembly); }

With the EF Core 2.2 preview package, how would I go about adding the Access Token value equivalently as the guide linked above?

public MyDatabaseContext(SqlConnection conn) : base(conn, true)
conn.ConnectionString = WebConfigurationManager.ConnectionStrings["MyDbConnection"].ConnectionString;
// DataSource != LocalDB means app is running in Azure with the SQLDB connection string you configured
if(conn.DataSource != "(localdb)\\MSSQLLocalDB")
    conn.AccessToken = (new AzureServiceTokenProvider()).GetAccessTokenAsync("").Result;


At initial glance, it looks like I would have to wait for the IdentityServer4.EntityFramework package to support this?

9/10/2018 9:46:30 PM

Popular Answer

I had a similar issue,

Try the overload of UseSqlServer that accepts a DbConnection, and pass it a SqlConnection with the token set.

It worked for me

6/25/2019 12:03:38 PM

Related Questions


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