Identity Server EF Core DB Configuration with Azure MSI

asp.net azure-sql-database entity-framework-core identityserver4

Question

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 (https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-connect-msi).

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 =>
                {
                    b.UseSqlServer(
                        connectionString,
                        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("https://database.windows.net/").Result;

Database.SetInitializer<MyDatabaseContext>(null);
}

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

1
2
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

0
6/25/2019 12:03:38 PM


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