How do I create a connection string with a secret token for a .NET Web API using EF Core app with the SQL Server backend that uses Microsoft Entra MFA
12:32 04 Nov 2025

I have a generic .NET Core Web API app that uses EF Core to connect to a SQL Server that uses Microsoft Entra MFA account

The connectivity prompt for the SQL Server looks like this

enter image description here

I have no problem connecting to this database locally using the following connection string and the code

string connectionString = "Server=tcp:some-server.database.windows.net,1433;Authentication=Active Directory Interactive;Database=some-database;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      => optionsBuilder.UseSqlServer(connectionString, builder =>
        {
            builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
        });`

However, every time I connect locally, a browser opens with a prompt where I have to enter my password

Obviously, that mode won't work once I deploy the app into an App Service or a container.

I know that I have to use a secret token and add Service Principal as SQL User somehow.

I have generated Service Principal Name, Secret, Application (Client) ID, Tenant ID, Subscription ID, Object ID, and the Directory ID:

How do I generate a proper connection string for this connection and what other steps I should take to properly deploy the app?

Thank you very much in advance

c# sql-server ef-code-first microsoft-entra-id