使用 .NET SDK 向 Azure Data Lake Storage Gen1 進行服務對服務驗證

在此文章中,您會了解如何使用 .NET SDK 向 Azure Data Lake Storage Gen1 進行服務對服務驗證。 針對使用 .NET SDK 向 Data Lake Storage Gen1 進行使用者驗證,請參閱使用 .NET SDK 向 Data Lake Storage Gen1 進行使用者驗證

必要條件

建立 .NET 應用程式

  1. 在 Visual Studio 中,選取 [檔案] 功能表、[新增] 及 [專案]。

  2. 選擇 [主控台應用程式 (.NET Framework)],然後選取 [下一步]。

  3. 在 [專案名稱] 中,輸入 CreateADLApplication,然後選取 [建立]

  4. 將 NuGet 套件新增至您的專案。

    1. 在方案總管中以滑鼠右鍵按一下專案名稱,然後按一下 [ 管理 NuGet 封裝]。

    2. 在 [NuGet 套件管理員] 索引標籤中,確定 [套件來源] 設為 [nuget.org],且已選取 [包含發行前版本] 核取方塊。

    3. 搜尋並安裝下列 NuGet 封裝:

      • Microsoft.Azure.Management.DataLake.Store - 本教學課程使用 v2.1.3-preview。

      • Microsoft.Rest.ClientRuntime.Azure.Authentication - 本教學課程使用 v2.2.12。

        新增 NuGet 來源

    4. 關閉 [NuGet 套件管理員]。

  5. 開啟 Program.cs,刪除現有的程式碼,然後納入下列陳述式以新增命名空間的參考。

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates; // Required only if you are using an Azure AD application created with certificates

using Microsoft.Rest;
using Microsoft.Rest.Azure.Authentication;
using Microsoft.Azure.Management.DataLake.Store;
using Microsoft.Azure.Management.DataLake.Store.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

使用用戶端祕密進行服務對服務驗證

在 .NET 用戶端應用程式中加入這個程式碼片段。 將佔位元元值取代為從 Microsoft Entra Web 應用程式擷取的值, (列為必要條件) 。 此代碼段可讓您使用適用於 Microsoft Entra Web 應用程式的用戶端密碼/金鑰,以非互動方式向 Data Lake Storage Gen1 驗證應用程式。

private static void Main(string[] args)
{
    // Service principal / application authentication with client secret / key
    // Use the client ID of an existing AAD "Web App" application.
    string TENANT = "<AAD-directory-domain>";
    string CLIENTID = "<AAD_WEB_APP_CLIENT_ID>";
    System.Uri ARM_TOKEN_AUDIENCE = new System.Uri(@"https://management.core.windows.net/");
    System.Uri ADL_TOKEN_AUDIENCE = new System.Uri(@"https://datalake.azure.net/");
    string secret_key = "<AAD_WEB_APP_SECRET_KEY>";
    var armCreds = GetCreds_SPI_SecretKey(TENANT, ARM_TOKEN_AUDIENCE, CLIENTID, secret_key);
    var adlCreds = GetCreds_SPI_SecretKey(TENANT, ADL_TOKEN_AUDIENCE, CLIENTID, secret_key);
}

前述程式碼片段會使用協助程式函式 GetCreds_SPI_SecretKey。 從 GitHub 即可取得此協助程式函式的程式碼。

使用憑證進行服務對服務驗證

在 .NET 用戶端應用程式中加入這個程式碼片段。 將佔位元元值取代為從 Microsoft Entra Web 應用程式擷取的值, (列為必要條件) 。 此代碼段可讓您使用 Microsoft Entra Web 應用程式的憑證,以非互動方式向 Data Lake Storage Gen1 驗證應用程式。 如需如何建立 Microsoft Entra 應用程式的指示,請參閱使用憑證建立服務主體

private static void Main(string[] args)
{
    // Service principal / application authentication with certificate
    // Use the client ID and certificate of an existing AAD "Web App" application.
    string TENANT = "<AAD-directory-domain>";
    string CLIENTID = "<AAD_WEB_APP_CLIENT_ID>";
    System.Uri ARM_TOKEN_AUDIENCE = new System.Uri(@"https://management.core.windows.net/");
    System.Uri ADL_TOKEN_AUDIENCE = new System.Uri(@"https://datalake.azure.net/");
    var cert = new X509Certificate2(@"d:\cert.pfx", "<certpassword>");
    var armCreds = GetCreds_SPI_Cert(TENANT, ARM_TOKEN_AUDIENCE, CLIENTID, cert);
    var adlCreds = GetCreds_SPI_Cert(TENANT, ADL_TOKEN_AUDIENCE, CLIENTID, cert);
}

前述程式碼片段會使用協助程式函式 GetCreds_SPI_Cert。 從 GitHub 即可取得此協助程式函式的程式碼。

下一步

在此文章中,您已了解如何使用 .NET SDK 透過服務對服務驗證向 Data Lake Storage Gen1 進行驗證。 您現在可以看看下列文章,了解如何使用 .NET SDK 搭配 Data Lake Storage Gen1。