Rediger

Del via


Integrate Azure SQL Database with Service Connector

This page shows supported authentication methods and clients, and shows sample code you can use to connect compute services to Azure SQL Database using Service Connector. You might still be able to connect to Azure SQL Database using other methods. This page also shows default environment variable names and values you get when you create the 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 table below 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 is not 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

This table indicates that the Secret/connection string method is supported for all client types. The System-assigned managed identity, User-assigned managed identity, and Service principal methods are supported for .NET, Java, Java - Spring Boot, Node.js, Python, and None client types. These methods are not supported for Go, PHP, Django, and Ruby client types.

Note

System-assigned managed identity,User-assigned managed identity and Service principal are only supported on Azure CLI.

Default environment variable names or application properties and sample code

Use the connection details below to connect compute services to Azure SQL Database. For each example below, 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

Refer to the steps and code below to connect to Azure SQL Database using a system-assigned managed identity.

  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

Refer to the steps and code below to connect to Azure SQL Database using a user-assigned managed identity.

  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 very high degree of trust in the application, and carries risks that are not 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

Refer to the steps and code below to connect to Azure SQL Database using a connection string.

  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

Refer to the steps and code below to connect to Azure SQL Database using a service principal.

  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

Follow the tutorial listed below to learn more about Service Connector.