使用 .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-预览版。

      • 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);
}

上述代码片段将使用 helper 函数 GetCreds_SPI_SecretKey。 可在此处(GitHub 上)获取此 helper 函数的代码。

使用证书的服务到服务身份验证

在 .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);
}

上述代码片段将使用 helper 函数 GetCreds_SPI_Cert。 可在此处(GitHub 上)获取此 helper 函数的代码。

后续步骤

本文介绍了如何通过 .NET SDK 使用服务到服务身份验证进行 Data Lake Storage Gen1 身份验证。 接下来,可以查看以下介绍如何使用 .NET SDK 在 Data Lake Storage Gen1 中执行操作的文章。