2

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://learn.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 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.