Edit

Share via


Integrate Azure SQL Database with Service Connector

In this article, we cover the supported authentication methods and clients that you can use to connect your apps to Azure SQL Database using Service Connector. For each supported method, we provide sample code and describe the default environment variable names, values, and configuration obtained when creating a service connection.

Supported compute services

Service Connector can be used to connect the following compute services to Azure SQL Database:

  • Azure App Service
  • Azure Container Apps
  • Azure Functions
  • Azure Kubernetes Service (AKS)
  • Azure Spring Apps

Supported authentication types and clients

The following table shows which combinations of authentication methods and clients are supported for connecting your compute service to Azure SQL Database using Service Connector. A Yes indicates that the combination is supported, while a No indicates that it isn't supported.

Client type System-assigned managed identity User-assigned managed identity Secret/connection string Service principal
.NET Yes Yes Yes Yes
Go No No Yes No
Java Yes Yes Yes Yes
Java - Spring Boot Yes Yes Yes Yes
Node.js Yes Yes Yes Yes
PHP No No Yes No
Python Yes Yes Yes Yes
Python - Django No No Yes No
Ruby No No Yes No
None Yes Yes Yes Yes

Note

System-assigned managed identity, user-assigned managed identity and service principal authentication is only supported on Azure CLI.

Default environment variable names or application properties and sample code

Use the following connection details to connect compute services to Azure SQL Database. For each example, replace the placeholder texts <sql-server>, <sql-database>, <sql-username>, and <sql-password> with your own server name, database name, user ID, and password. For more information about naming conventions, check the Service Connector internals article.

System-assigned managed identity

Default environment variable name Description Sample value
AZURE_SQL_CONNECTIONSTRING Azure SQL Database connection string Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;Authentication=ActiveDirectoryManagedIdentity

Sample code

To connect to Azure SQL Database using a system-assigned managed identity, refer to the following steps and sample code.

  1. Install dependencies.

    dotnet add package Microsoft.Data.SqlClient
    
  2. Get the Azure SQL Database connection string from the environment variable added by Service Connector.

    using Microsoft.Data.SqlClient;
    
    string connectionString = 
        Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
    
    using var connection = new SqlConnection(connectionString);
    connection.Open();
    

    For more information, see Using Active Directory Managed Identity authentication.

For more information, see Homepage for client programming to Microsoft SQL Server.

User-assigned managed identity

Default environment variable name Description Sample value
AZURE_SQL_CONNECTIONSTRING Azure SQL Database connection string Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;User ID=<identity-client-ID>;Authentication=ActiveDirectoryManagedIdentity

Sample code

To connect to Azure SQL Database using a user-assigned managed identity, refer to the following steps and sample code.

  1. Install dependencies.

    dotnet add package Microsoft.Data.SqlClient
    
  2. Get the Azure SQL Database connection string from the environment variable added by Service Connector.

    using Microsoft.Data.SqlClient;
    
    string connectionString = 
        Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
    
    using var connection = new SqlConnection(connectionString);
    connection.Open();
    

    For more information, see Using Active Directory Managed Identity authentication.

For more information, see Homepage for client programming to Microsoft SQL Server.

Connection string

Warning

Microsoft recommends that you use the most secure authentication flow available. The authentication flow described in this procedure requires a high degree of trust in the application, and carries risks that aren't present in other flows. You should only use this flow when other more secure flows, such as managed identities, aren't viable.

Default environment variable name Description Sample value
AZURE_SQL_CONNECTIONSTRING Azure SQL Database connection string Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;Password=<sql-password>

Sample code

To connect to Azure SQL Database using a connection string, refer to the following steps and sample code.

  1. Install dependencies.

    dotnet add package Microsoft.Data.SqlClient
    
  2. Get the Azure SQL Database connection string from the environment variable added by Service Connector.

    using Microsoft.Data.SqlClient;
    
    string connectionString = 
        Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
    
    using var connection = new SqlConnection(connectionString);
    connection.Open();
    

For more information, see Homepage for client programming to Microsoft SQL Server.

Service principal

Default environment variable name Description Example value
AZURE_SQL_CLIENTID Your client ID <client-ID>
AZURE_SQL_CLIENTSECRET Your client secret <client-secret>
AZURE_SQL_TENANTID Your tenant ID <tenant-ID>
AZURE_SQL_CONNECTIONSTRING Azure SQL Database connection string Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;User ID=<client-Id>;Password=<client-secret>;Authentication=ActiveDirectoryServicePrincipal

Sample code

To connect to Azure SQL Database using a service principal, refer to the following steps and sample code.

  1. Install dependencies.

    dotnet add package Microsoft.Data.SqlClient
    
  2. Get the Azure SQL Database connection string from the environment variable added by Service Connector.

    using Microsoft.Data.SqlClient;
    
    string connectionString = 
        Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
    
    using var connection = new SqlConnection(connectionString);
    connection.Open();
    

    For more information, see Using Active Directory Managed Identity authentication.

For more information, see Homepage for client programming to Microsoft SQL Server.

Next steps

To learn more about Service Connector, see the following tutorial.