Use DistributedSqlServerCache with an MSI in Azure SQL Db and remove credentials from Connection String

Steven Bitaxi 1 Reputation point
2021-03-15T16:03:15.177+00:00

Good morning all, does anyone have an example implementation using MSI authentication for an Azure SQL Db used as a token cache?
I'm trying to get the credentials out of the connection string and use a token but there doesn't seem to be a place to pass it in:

        services.AddMicrosoftIdentityWebAppAuthentication(Configuration)
            .EnableTokenAcquisitionToCallDownstreamApi(new[] { "https://database.windows.net//.default" })
            .AddDistributedTokenCaches();

        services.AddDistributedSqlServerCache(options =>
        {
            options.ConnectionString = 
                Configuration.GetConnectionString("MyDbConnection");
            options.SchemaName = "dbo";
            options.TableName = "App_Token_Cache";
            options.DefaultSlidingExpiration = TimeSpan.FromMinutes(90);
        });

Do I need to create my own Cache service to accomplish this?

Azure SQL Database
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,874 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Steven Bitaxi 1 Reputation point
    2021-03-19T13:57:41.68+00:00

    Thank you very much Marilee. The AAD configuration and permissions are in place. A user of the 425Show forum advised me that I needed to use the correct connection string.

    According to https://learn.microsoft.com/en-us/sql/connect/ado-net/sql/azure-active-directory-authentication?view=sql-server-ver15#using-active-directory-managed-identity-authentication the service will get the token. I don't need to handle it in the application.

    "Server=AZURESQLSERVER.database.windows.net,1433;Database=AZURE_DB;Authentication=Active Directory MSI;"  
    

  2. Patel, Saurabh K 0 Reputation points
    2024-01-31T21:11:38.94+00:00

    Below code is not working with this connection string. Do I miss anything?? Connection string "Server=AZURESQLSERVER.database.windows.net,1433;Database=AZURE_DB;Authentication=Active Directory MSI;"

    services.AddDistributedSqlServerCache(options =>
        {                
                    options.ConnectionString = CRAFDBConnection;                
                    options.SchemaName = "dbo";
                    options.TableName = "SQLSessions";
       });