DataLakeServiceClient使用TokenCredentials引发身份验证错误

时间:2020-04-21 18:57:20

标签: azure token azure-data-lake

从DataLakeServiceClient读取将导致:

服务器无法验证请求。确保正确构成Authorization标头的值,包括签名

当我使用通过AAD获取的令牌创建的TokenCredentials时,会发生这种情况。

注意:如果我使用StorageSharedKeyCredential并指定了帐户密钥,那么一切正常,我可以读取数据。

如何使它与TokenCredentials一起使用?我想验证用户是否具有读取直接文件的权限,而不是使用帐户密钥来规避此安全检查。

public DataLakeServiceClient GetDataLakeServiceClient(string token)
{
    string accountName = "***";
    AzureTokenCrential azureTokenCredential = new AzureTokenCrential(token);
    string dfsUri = "***";
    DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClient(new Uri(dfsUri), azureTokenCredential);

    return dataLakeServiceClient;
}

DataLakeServiceClient client = this.GetDataLakeServiceClient(token);
DataLakeFileSystemClient fileSystemClient = client.GetFileSystemClient("");
DataLakeDirectoryClient directoryClient = fileSystemClient.GetDirectoryClient("TestBlobContainer");
DataLakeFileClient fileClient = directoryClient.GetFileClient("TestFile.txt");
Response<FileDownloadInfo> downloadResponse = await fileClient.ReadAsync();
TextReader textReader = new StreamReader(downloadResponse.Value.Content);
string text = textReader.ReadToEnd();

谢谢!

-麦克

1 个答案:

答案 0 :(得分:0)

您可以使用Azure identity client library for .NET通过Azure AD对应用程序进行身份验证。

此示例使用客户端ID,客户端机密和租户ID创建DataLakeServiceClient实例。要获取这些值,请参阅从Azure AD for authorizing requests from a client application获取令牌。

public void GetDataLakeServiceClient(ref DataLakeServiceClient dataLakeServiceClient, 
    String accountName, String clientID, string clientSecret, string tenantID)
{

    TokenCredential credential = new ClientSecretCredential(
        tenantID, clientID, clientSecret, new TokenCredentialOptions());

    string dfsUri = "https://" + accountName + ".dfs.core.windows.net";

    dataLakeServiceClient = new DataLakeServiceClient(new Uri(dfsUri), credential);
}

有关更多详细信息,您可以参阅有关connecting by using Azure Active Directory的这篇文章。