【问题标题】:Version conflict detected for Microsoft.IdentityModel.Clients.ActiveDirectory检测到 Microsoft.IdentityModel.Clients.ActiveDirectory 的版本冲突
【发布时间】:2017-11-09 02:27:08
【问题描述】:
创建一个 Azure 函数,该函数使用 nuget 包 Microsoft.Rest.ClientRuntime.Azure.Authentication 对 DataLake 进行身份验证,并使用 Microsoft.IdentityModel.Clients.ActiveDirectory 对 HDInsight 进行身份验证。当我尝试在函数项目中安装两者时出现以下错误:
uninstall-package:检测到 Microsoft.IdentityModel.Clients.ActiveDirectory 的版本冲突。直接从项目中引用包来解决这个问题
问题。 MyProject.Functions (>= 1.0.0) -> Microsoft.Rest.ClientRuntime.Azure.Authentication (>= 2.3.1) -> Microsoft.IdentityModel.Clients.ActiveDirectory (>= 2.28.3)
MyProject.Functions (>= 1.0.0) -> Microsoft.Azure.Common.Authentication (>= 1.7.0-preview) -> Microsoft.IdentityModel.Clients.ActiveDirectory (>=2.18.206251556)。
看起来 Microsoft.Azure.Common.Authentication 1.7.0-preview 对仅引用 Microsoft.IdentityModel.Clients.ActiveDirectory 2.18.206251556 有限制。不幸的是,这个库自 2016 年 2 月以来一直没有更新,除了https://docs.microsoft.com/en-us/azure/hdinsight/hdinsight-create-non-interactive-authentication-dotnet-applications 中概述的步骤之外,我不确定另一种使用 HDInsight 进行非交互式身份验证的方法。
【问题讨论】:
标签:
c#
.net
azure
azure-hdinsight
azure-data-lake
【解决方案1】:
据我了解,您可以直接使用包Microsoft.IdentityModel.Clients.ActiveDirectory 来检索访问令牌,而不是使用Microsoft.Azure.Common.Authentication 包。
根据您的描述,我创建了我的 azure 函数项目来测试这个问题。我安装的包如下:
获取令牌的方法:
private static string GetAuthorizationToken()
{
string tenantId = "xxx";
string clientId = "xxx";
string clientSecrets = "xxx";
var context = new AuthenticationContext(String.Format("https://login.windows.net/{0}", tenantId));
AuthenticationResult result = context.AcquireTokenAsync(
"https://management.azure.com/"
, new ClientCredential(clientId, clientSecrets)
).Result;
return result.AccessToken;
}
我的功能:
[FunctionName("Function1")]
public static void Run([TimerTrigger("*/10 * * * * *")]TimerInfo myTimer,TraceWriter log)
{
TokenCloudCredentials tokenCredential = new TokenCloudCredentials("{subscriptionId}", GetAuthorizationToken());
HDInsightManagementClient _hdiManagementClient = new HDInsightManagementClient(tokenCredential);
var results = _hdiManagementClient.Clusters.List();
foreach (var name in results.Clusters)
{
Console.WriteLine("Cluster Name: " + name.Name);
Console.WriteLine("\t Cluster type: " + name.Properties.ClusterDefinition.ClusterType);
Console.WriteLine("\t Cluster location: " + name.Location);
Console.WriteLine("\t Cluster version: " + name.Properties.ClusterVersion);
}
}
【解决方案2】:
在尝试以几种不同的方式解决依赖关系后,我接受了 Bruce 的建议,删除了对 Microsoft.Azure.Common.Authentication 的所有引用,并改用 Microsoft.IdentityModel.Clients.ActiveDirectory 来获取令牌。