使用 Microsoft Entra 身份验证进行连接

下载 PHP 驱动程序

Microsoft Entra ID (Azure AD) 是一项中心用户 ID 管理技术,可以作为 SQL Server 身份验证的替代方法。 Microsoft Entra ID 允许使用用户名和密码、Windows 集成身份验证或 Microsoft Entra ID 访问令牌在 Microsoft Entra ID 中使用联合身份连接到 Microsoft Azure SQL 数据库、Azure SQL 托管实例及 Azure Synapse Analytics。 适用于 SQL Server 的 PHP 驱动程序提供对这些功能的部分支持。

使用 Microsoft Entra 身份验证之前,须使用 Azure SQL 配置和管理 Microsoft Entra 身份验证

若要使用 Microsoft Entra ID,请使用“Authentication”或“AccessToken”关键字(它们互相排斥),如下表所示。 有关更多技术详细信息,请参阅结合使用 Microsoft Entra ID 和 ODBC 驱动程序

关键字 说明
AccessToken 未设置(默认值) 身份验证模式由其他关键字确定。 有关详细信息,请参阅 Connection Options
一个字节字符串 从 OAuth JSON 响应中提取的 Microsoft Entra 访问令牌。 连接字符串不能包含用户 ID、密码或 Authentication 关键字(在 Linux 或 macOS 中需要 ODBC Driver 版本 17 或更高版本)。
身份验证 未设置(默认值) 身份验证模式由其他关键字确定。 有关详细信息,请参阅 Connection Options
SqlPassword 使用用户名和密码直接对 SQL Server 实例(可能是 Azure 实例)进行身份验证。 必须使用“UID” 和“PWD” 关键字将用户名和密码传递到连接字符串。
ActiveDirectoryPassword 使用用户名和密码对 Microsoft Entra 标识进行身份验证。 必须使用“UID” 和“PWD” 关键字将用户名和密码传递到连接字符串。
ActiveDirectoryMsi 使用 Microsoft Entra 系统分配的托管标识或用户分配的托管标识进行身份验证(需要 ODBC Driver 版本 17.3.1.1 或更高版本)。 有关概述和教程,请参阅 什么是 Azure 资源的托管标识?
ActiveDirectoryServicePrincipal 使用服务主体对象进行身份验证(需要 ODBC 驱动程序版本 17.7 或更高版本)。 有关详细信息,请参阅 Microsoft Entra ID 中的应用程序和服务主体对象

“Authentication” 关键字影响连接安全设置。 如果在连接字符串中设置了此关键字,则默认情况下“Encrypt” 关键字设置为 true,这意味着客户端将请求加密。 而且,除非 TrustServerCertificate 设置为 true(默认情况下为 false ),否则将验证服务器证书,而不考虑加密设置。 此功能有别于旧的、不安全的登录方法,在此方法中,仅当在连接字符串中明确请求加密时,才会验证服务器证书。

限制

在 Windows 上,基础 ODBC 驱动程序支持“Authentication” 关键字的另一个值“ActiveDirectoryIntegrated” ,但 PHP 驱动程序在任何平台上都不支持这个值。

示例 - 使用 SqlPassword 和 ActiveDirectoryPassword 进行连接

<?php
// First connect to a local SQL Server instance by setting Authentication to SqlPassword
$serverName = "myserver.mydomain";

$connectionInfo = array("UID"=>$myusername, "PWD"=>$mypassword, "Authentication"=>'SqlPassword');

$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn === false) {
    echo "Could not connect with Authentication=SqlPassword.\n";
    print_r(sqlsrv_errors());
} else {
    echo "Connected successfully with Authentication=SqlPassword.\n";
    sqlsrv_close($conn);
}

// Now connect to an Azure SQL database by setting Authentication to ActiveDirectoryPassword
$azureServer = "myazureserver.database.windows.net";
$azureDatabase = "myazuredatabase";
$azureUsername = "myuid";
$azurePassword = "mypassword";
$connectionInfo = array("Database"=>$azureDatabase,
                        "UID"=>$azureUsername,
                        "PWD"=>$azurePassword,
                        "Authentication"=>'ActiveDirectoryPassword');

$conn = sqlsrv_connect($azureServer, $connectionInfo);
if ($conn === false) {
    echo "Could not connect with Authentication=ActiveDirectoryPassword.\n";
    print_r(sqlsrv_errors());
} else {
    echo "Connected successfully with Authentication=ActiveDirectoryPassword.\n";
    sqlsrv_close($conn);
}

?>

示例 - 使用 PDO_SQLSRV 驱动程序进行连接

<?php
// First connect to a local SQL Server instance by setting Authentication to SqlPassword
$serverName = "myserver.mydomain";

$connectionInfo = "Database = $databaseName; Authentication = SqlPassword;";

try {
    $conn = new PDO("sqlsrv:server = $serverName ; $connectionInfo", $myusername, $mypassword);
    echo "Connected successfully with Authentication=SqlPassword.\n";
    $conn = null;
} catch (PDOException $e) {
    echo "Could not connect with Authentication=SqlPassword.\n";
    print_r($e->getMessage());
    echo "\n";
}

// Now connect to an Azure SQL database by setting Authentication to ActiveDirectoryPassword
$azureServer = "myazureserver.database.windows.net";
$azureDatabase = "myazuredatabase";
$azureUsername = "myuid";
$azurePassword = "mypassword";
$connectionInfo = "Database = $azureDatabase; Authentication = ActiveDirectoryPassword;";

try {
    $conn = new PDO("sqlsrv:server = $azureServer ; $connectionInfo", $azureUsername, $azurePassword);
    echo "Connected successfully with Authentication=ActiveDirectoryPassword.\n";
    unset($conn);
} catch (PDOException $e) {
    echo "Could not connect with Authentication=ActiveDirectoryPassword.\n";
    print_r($e->getMessage());
    echo "\n";
}
?>

示例 – 使用 Microsoft Entra 访问令牌进行连接

SQLSRV 驱动程序

<?php
// Using an access token to connect: do not use UID or PWD connection options
// Assume $accToken is the valid byte string extracted from an OAuth JSON response
$connectionInfo = array("Database"=>$azureAdDatabase, "AccessToken"=>$accToken);
$conn = sqlsrv_connect($azureAdServer, $connectionInfo);
if ($conn === false) {
    echo "Could not connect with Azure AD Access Token.\n";
    print_r(sqlsrv_errors());
} else {
    echo "Connected successfully with Azure AD Access Token.\n";
    sqlsrv_close($conn);
}
?>

PDO_SQLSRV 驱动程序

<?php
try {
    // Using an access token to connect: do not pass in $uid or $pwd
    // Assume $accToken is the valid byte string extracted from an OAuth JSON response
    $connectionInfo = "Database = $azureAdDatabase; AccessToken = $accToken;";
    $conn = new PDO("sqlsrv:server = $azureAdServer; $connectionInfo");
    echo "Connected successfully with Azure AD Access Token\n";
    unset($conn);
} catch (PDOException $e) {
    echo "Could not connect with Azure AD Access Token.\n";
    print_r($e->getMessage());
    echo "\n";
}
?>

示例 - 使用 Azure 资源的托管标识进行连接

对 SQLSRV 驱动程序使用系统分配的托管标识

使用系统分配的托管标识进行连接时,请不要使用 UID 或 PWD 选项。

<?php

$azureServer = 'myazureserver.database.windows.net';
$azureDatabase = 'myazuredatabase';
$connectionInfo = array('Database'=>$azureDatabase,
                        'Authentication'=>'ActiveDirectoryMsi');
$conn = sqlsrv_connect($azureServer, $connectionInfo);

if ($conn === false) {
    echo "Could not connect with Authentication=ActiveDirectoryMsi (system-assigned).\n";
    print_r(sqlsrv_errors());
} else {
    echo "Connected successfully with Authentication=ActiveDirectoryMsi (system-assigned).\n";
    
    $tsql = "SELECT @@Version AS SQL_VERSION";
    $stmt = sqlsrv_query($conn, $tsql);
    if ($stmt === false) {
        echo "Failed to run the simple query (system-assigned).\n";
        print_r(sqlsrv_errors());
    } else {
        while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
            echo $row['SQL_VERSION'] . PHP_EOL;
        }

        sqlsrv_free_stmt($stmt);
    }
    
    sqlsrv_close($conn);
}
?>

示例 – 使用 Microsoft Entra 服务主体进行连接

若要使用服务主体对象进行身份验证,将需要相应的应用程序客户端 ID客户端密码

SQLSRV 驱动程序

<?php

$adServer = 'myazureserver.database.windows.net';
$adDatabase = 'myazuredatabase';
$adSPClientId = 'myAppClientId';
$adSPClientSecret = 'myClientSecret';

$conn = false;
$connectionInfo = array("Database"=>$adDatabase, 
                        "Authentication"=>"ActiveDirectoryServicePrincipal",
                        "UID"=>$adSPClientId,
                        "PWD"=>$adSPClientSecret);

$conn = sqlsrv_connect($adServer, $connectionInfo);
if ($conn === false) {
    echo "Could not connect using Azure AD Service Principal." . PHP_EOL;
    print_r(sqlsrv_errors());
}

sqlsrv_close($conn);

?>

PDO_SQLSRV 驱动程序

<?php

$adServer = 'myazureserver.database.windows.net';
$adDatabase = 'myazuredatabase';
$adSPClientId = 'myAppClientId';
$adSPClientSecret = 'myClientSecret';

$conn = false;
try {
    $connectionInfo = "Database = $adDatabase; Authentication = ActiveDirectoryServicePrincipal;";
    $conn = new PDO("sqlsrv:server = $adServer; $connectionInfo", $adSPClientId, $adSPClientSecret);
} catch (PDOException $e) {
    echo "Could not connect using Azure AD Service Principal.\n";
    print_r($e->getMessage());
    echo PHP_EOL;
}

unset($conn);
?>

另请参阅

结合使用 Microsoft Entra ID 与 ODBC 驱动程序

什么是 Azure 资源的托管标识?