共用方式為


使用 Microsoft Entra ID

適用於:SQL Server Azure SQL 資料庫 Azure SQL 受控執行個體 Azure Synapse Analytics Analytics Platform System (PDW) Microsoft Fabric 的 SQL 端點分析 Microsoft Fabric 的倉儲

下載 OLE DB 驅動程式

目的

18.2.1 版開始,Microsoft OLE DB Driver for SQL Server 可讓 OLE DB 應用程式使用同盟身分識別,連線到 Azure SQL 資料庫、Azure SQL 受控執行個體、Azure Synapse Analytics 與 Microsoft Fabric。

Microsoft Entra 驗證方法包括:

  • 使用者名稱與密碼
  • 存取權杖
  • 整合式驗證

18.3.0 版增加對下列 Microsoft Entra 驗證方法的支援:

18.5.0 版增加對下列驗證方法的支援:

  • Microsoft Entra 服務主體驗證

注意

支援在 DataTypeCompatibility (或其對應的屬性) 設定為 80 的情況下使用下列驗證模式:

  • 以使用者名稱和密碼進行 Microsoft Entra 驗證
  • 使用存取權杖進行 Microsoft Entra 驗驗證
  • Microsoft Entra 整合式驗證
  • Microsoft Entra 互動式驗證
  • Microsoft Entra 受控識別驗證
  • Microsoft Entra 服務主體驗證

如需使用 Microsoft Entra 驗證,您必須設定 Azure SQL 資料來源。 如需詳細資訊,請參閱使用 Azure SQL 設定和管理 Microsoft Entra 驗證

連接字串的關鍵字及屬性

已引入下列連接字串關鍵字來支援 Microsoft Entra 驗證:

連接字串關鍵字 Connection 屬性 描述
存取權杖 SSPROP_AUTH_ACCESS_TOKEN 指定要向 Microsoft Entra ID 進行驗證的存取權杖。
驗證 SSPROP_AUTH_MODE 指定要使用的驗證方法。

如需有關新關鍵字/屬性的詳細資訊,請參閱下列頁面:

加密和憑證驗證

如需詳細資訊,請參閱加密和憑證驗證

GUI 新增項目

驅動程式圖形化使用者介面已經過增強,可允許 Microsoft Entra 驗證。 如需詳細資訊,請參閱

範例連接字串

本節顯示新連接字串關鍵字和現有連接字串關鍵字的範例,以搭配 IDataInitialize::GetDataSourceDBPROP_INIT_PROVIDERSTRING 屬性使用。

SQL 驗證

  • 使用: IDataInitialize::GetDataSource
    • 新:

      Provider=MSOLEDBSQL19;Data Source=[server];Initial Catalog=[database];Authentication=SqlPassword;User ID=[username];Password=[password];Use Encryption for Data=Mandatory

    • 取代:

      Provider=MSOLEDBSQL19;Data Source=[server];Initial Catalog=[database];User ID=[username];Password=[password];Use Encryption for Data=Mandatory

  • 使用: DBPROP_INIT_PROVIDERSTRING
    • 新:

      Server=[server];Database=[database];Authentication=SqlPassword;UID=[username];PWD=[password];Encrypt=Mandatory

    • 取代:

      Server=[server];Database=[database];UID=[username];PWD=[password];Encrypt=Mandatory

使用安全性支援提供者介面 (SSPI) 進行整合式 Windows 驗證

  • 使用: IDataInitialize::GetDataSource
    • 新:

      Provider=MSOLEDBSQL19;Data Source=[server];Initial Catalog=[database];Authentication=ActiveDirectoryIntegrated;Use Encryption for Data=Mandatory

    • 取代:

      Provider=MSOLEDBSQL19;Data Source=[server];Initial Catalog=[database];Integrated Security=SSPI;Use Encryption for Data=Mandatory

  • 使用: DBPROP_INIT_PROVIDERSTRING
    • 新:

      Server=[server];Database=[database];Authentication=ActiveDirectoryIntegrated;Encrypt=Mandatory

    • 取代:

      Server=[server];Database=[database];Trusted_Connection=yes;Encrypt=Mandatory

Microsoft Entra 使用者名稱和密碼驗證。

  • 使用: IDataInitialize::GetDataSource

    Provider=MSOLEDBSQL19;Data Source=[server];Initial Catalog=[database];Authentication=ActiveDirectoryPassword;User ID=[username];Password=[password];Use Encryption for Data=Mandatory

  • 使用: DBPROP_INIT_PROVIDERSTRING

    Server=[server];Database=[database];Authentication=ActiveDirectoryPassword;UID=[username];PWD=[password];Encrypt=Mandatory

Microsoft Entra 整合式驗證

  • 使用: IDataInitialize::GetDataSource

    Provider=MSOLEDBSQL19;Data Source=[server];Initial Catalog=[database];Authentication=ActiveDirectoryIntegrated;Use Encryption for Data=Mandatory

  • 使用: DBPROP_INIT_PROVIDERSTRING

    Server=[server];Database=[database];Authentication=ActiveDirectoryIntegrated;Encrypt=Mandatory

使用存取權杖進行 Microsoft Entra 驗證

  • 使用: IDataInitialize::GetDataSource

    Provider=MSOLEDBSQL19;Data Source=[server];Initial Catalog=[database];Access Token=[access token];Use Encryption for Data=Mandatory

  • 使用: DBPROP_INIT_PROVIDERSTRING

    不支援透過 DBPROP_INIT_PROVIDERSTRING 提供存取權杖

Microsoft Entra 互動式驗證

  • 使用: IDataInitialize::GetDataSource

    Provider=MSOLEDBSQL19;Data Source=[server];Initial Catalog=[database];Authentication=ActiveDirectoryInteractive;User ID=[username];Use Encryption for Data=Mandatory

  • 使用: DBPROP_INIT_PROVIDERSTRING

    Server=[server];Database=[database];Authentication=ActiveDirectoryInteractive;UID=[username];Encrypt=Mandatory

Microsoft Entra 受控識別驗證。

  • 使用: IDataInitialize::GetDataSource
    • 使用者指派的受控識別:

      Provider=MSOLEDBSQL19;Data Source=[server];Initial Catalog=[database];Authentication=ActiveDirectoryMSI;User ID=[Object ID];Use Encryption for Data=Mandatory

    • 系統指派的受控識別:

      Provider=MSOLEDBSQL19;Data Source=[server];Initial Catalog=[database];Authentication=ActiveDirectoryMSI;Use Encryption for Data=Mandatory

  • 使用: DBPROP_INIT_PROVIDERSTRING
    • 使用者指派的受控識別:

      Server=[server];Database=[database];Authentication=ActiveDirectoryMSI;UID=[Object ID];Encrypt=Mandatory

    • 系統指派的受控識別:

      Server=[server];Database=[database];Authentication=ActiveDirectoryMSI;Encrypt=Mandatory

Microsoft Entra 服務主體驗證

  • 使用: IDataInitialize::GetDataSource

    Provider=MSOLEDBSQL19;Data Source=[server];Initial Catalog=[database];Authentication=ActiveDirectoryServicePrincipal;User ID=[Application (client) ID];Password=[Application (client) secret];Use Encryption for Data=Mandatory

  • 使用: DBPROP_INIT_PROVIDERSTRING

    Server=[server];Database=[database];Authentication=ActiveDirectoryServicePrincipal;UID=[Application (client) ID];PWD=[Application (client) secret];Encrypt=Mandatory

程式碼範例

下列範例顯示使用連接關鍵字連線到 Microsoft Entra ID 所需的程式碼。

存取權杖

#include <string>
#include <iostream>
#include <msdasc.h>

int main()
{
    wchar_t azureServer[] = L"server";
    wchar_t azureDatabase[] = L"mydatabase";
    wchar_t accessToken[] = L"eyJ0eXAiOi...";
    IDBInitialize *pIDBInitialize = nullptr;
    IDataInitialize* pIDataInitialize = nullptr;
    HRESULT hr = S_OK;

    CoInitialize(nullptr);

    // Construct the connection string.
    std::wstring connString = L"Provider=MSOLEDBSQL19;Data Source=" + std::wstring(azureServer) + L";Initial Catalog=" + 
                              std::wstring(azureDatabase) + L";Access Token=" + accessToken + L";Use Encryption for Data=Mandatory;";
    hr = CoCreateInstance(CLSID_MSDAINITIALIZE, nullptr, CLSCTX_INPROC_SERVER, 
                          IID_IDataInitialize, reinterpret_cast<LPVOID*>(&pIDataInitialize));
    if (FAILED(hr))
    {
        std::cout << "Failed to create an IDataInitialize instance." << std::endl;
        goto Cleanup;
    }
    hr = pIDataInitialize->GetDataSource(nullptr, CLSCTX_INPROC_SERVER, connString.c_str(), 
                                         IID_IDBInitialize, reinterpret_cast<IUnknown**>(&pIDBInitialize));
    if (FAILED(hr))
    {
        std::cout << "Failed to get data source object." << std::endl;
        goto Cleanup;
    }
    hr = pIDBInitialize->Initialize();
    if (FAILED(hr))
    {
        std::cout << "Failed to establish connection." << std::endl;
        goto Cleanup;
    }

Cleanup:
    if (pIDBInitialize)
    {
        pIDBInitialize->Uninitialize();
        pIDBInitialize->Release();
    }
    if (pIDataInitialize)
    {
        pIDataInitialize->Release();
    }

    CoUninitialize();
}

Active Directory 整合式

#include <string>
#include <iostream>
#include <msdasc.h>

int main()
{
    wchar_t azureServer[] = L"server";
    wchar_t azureDatabase[] = L"mydatabase";
    IDBInitialize *pIDBInitialize = nullptr;
    IDataInitialize* pIDataInitialize = nullptr;
    HRESULT hr = S_OK;

    CoInitialize(nullptr);

    // Construct the connection string.
    std::wstring connString = L"Provider=MSOLEDBSQL19;Data Source=" + std::wstring(azureServer) + L";Initial Catalog=" + 
                              std::wstring(azureDatabase) + L";Authentication=ActiveDirectoryIntegrated;Use Encryption for Data=Mandatory;";

    hr = CoCreateInstance(CLSID_MSDAINITIALIZE, nullptr, CLSCTX_INPROC_SERVER, 
                          IID_IDataInitialize, reinterpret_cast<LPVOID*>(&pIDataInitialize));
    if (FAILED(hr)) 
    {
        std::cout << "Failed to create an IDataInitialize instance." << std::endl;
        goto Cleanup;
    }
    hr = pIDataInitialize->GetDataSource(nullptr, CLSCTX_INPROC_SERVER, connString.c_str(), 
                                         IID_IDBInitialize, reinterpret_cast<IUnknown**>(&pIDBInitialize));
    if (FAILED(hr))
    {
        std::cout << "Failed to get data source object." << std::endl;
        goto Cleanup;
    }
    hr = pIDBInitialize->Initialize();
    if (FAILED(hr))
    {
        std::cout << "Failed to establish connection." << std::endl;
        goto Cleanup;
    }

Cleanup:
    if (pIDBInitialize)
    {
        pIDBInitialize->Uninitialize();
        pIDBInitialize->Release();
    }
    if (pIDataInitialize)
    {
        pIDataInitialize->Release();
    }

    CoUninitialize();
}