缺少程序集参考使用Microsoft.IdentityModel.Clients.ActiveDirectory 3.13.9与Azure KeyVault C#SDK

时间:2017-05-19 17:12:39

标签: c# .net azure nuget azure-keyvault

出于某种原因,我必须使用Microsoft.IdentityModel.Clients.ActiveDirectory v3.13.9进行最新Microsoft.Azure.KeyVault v2.0.6。并且它总是抛出缺少程序集引用的异常,但是将Microsoft.IdentityModel.Clients.ActiveDirectory降级到v2.28.4(我不允许使用它)可以解析此known issue

以下是例外:

  InnerException:
   HResult=-2146233036
   Message=The type initializer for 'Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformPlugin' threw an exception.
   Source=Microsoft.IdentityModel.Clients.ActiveDirectory
   TypeName=Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformPlugin
   StackTrace:
        at Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformPlugin.get_Logger()
        at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext..cctor()
   InnerException:
        ErrorCode=assembly_not_found
        HResult=-2146233088
        Message=Assembly required for the platform not found. Make sure assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory.Platform, Version=3.12.0.827, Culture=neutral, PublicKeyToken=31bf3856ad364e35' exists
        Source=Microsoft.IdentityModel.Clients.ActiveDirectory
        StackTrace:
             at Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformPlugin.LoadPlatformSpecificAssembly()
             at Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformPlugin.InitializeByAssemblyDynamicLinking()
             at Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformPlugin..cctor()
        InnerException:
             FileName=Microsoft.IdentityModel.Clients.ActiveDirectory.Platform, Version=3.12.0.827, Culture=neutral, PublicKeyToken=31bf3856ad364e35
             FusionLog==== Pre-bind state information ===

代码段:

    protected KeyVaultBase(string clientId, string clientSecret)
    {
        if (string.IsNullOrWhiteSpace(clientId))
        {
            throw new ArgumentNullException(nameof(clientId));
        }

        if (string.IsNullOrWhiteSpace(clientSecret))
        {
            throw new ArgumentNullException(nameof(clientSecret));
        }

        this.KeyVaultClient = new KeyVaultClient(
            new KeyVaultClient.AuthenticationCallback(
                (authority, resource, scope) => GetAccessToken(authority, resource, scope, clientId, clientSecret)
            )
        );
    }

    private static async Task<string> GetAccessToken(string authority, string resource, string scope, string clientId, string clientSecret)
    {
        var authnContext = new AuthenticationContext(authority);
        var clientCredential = new ClientCredential(clientId, clientSecret);
        var result = await authnContext.AcquireTokenAsync(resource, clientCredential);

        if (result == null)
        {
            throw new InvalidOperationException("Failed to obtain the JWT token");
        }

        return result.AccessToken;
    }

执行var authnContext = new AuthenticationContext(authority);时抛出异常。

有没有人知道在没有降级的情况下解决此问题的任何解决方案/解决方法?

2 个答案:

答案 0 :(得分:2)

在我的情况下,references仅安装到class library project,而不是安装assembly,因此它会抛出此异常。

Microsoft.IdentityModel.Clients.ActiveDirectory nuget包添加到正在执行的程序集中。

希望有所帮助

答案 1 :(得分:0)

请尝试卸载 Microsoft.IdentityModel.Clients.ActiveDirectory v3.13.9和Microsoft.Azure.KeyVault v2.0.6,然后再次重新安装。如果解决方案中有多个项目被引用,请确保它们都是相同的版本。

我创建了一个控制台或Asp.net MVC项目,两者都使用Microsoft.IdentityModel.Clients.ActiveDirectory v3.13.9与Microsoft.Azure.KeyVault v2.0.6正常工作。

以下是我的详细步骤:

1.创建一个C#控制台项目。

2.使用nuget添加Microsoft.IdentityModel.Clients.ActiveDirectory v3.13.9。

3.使用命令

从程序包管理器控制台添加Microsoft.Azure.KeyVault v2.0.6
Install-Package Microsoft.Azure.KeyVault -Version 2.0.6

Packages.config文件:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Azure.KeyVault" version="2.0.6" targetFramework="net452" />
  <package id="Microsoft.Azure.KeyVault.WebKey" version="2.0.4" targetFramework="net452" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.9" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.2" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.1" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net452" />
</packages>

在项目中添加一个类:

 class KeyVaultBase
    {

        static string _clientId= "xxxxxxxxxxxxx";
        static string _clientSecret = "xxxxxxxxxxxxxxxx";
        static string _tenantId = "xxxxxxxxxxxxxxxx";
        public static async Task<string> GetAccessToken(string azureTenantId, string azureAppId, string azureSecretKey)
        {

            var context = new AuthenticationContext("https://login.windows.net/" + _tenantId);
            ClientCredential clientCredential = new ClientCredential(_clientId, _clientSecret);
            var tokenResponse = await context.AcquireTokenAsync("https://vault.azure.net", clientCredential);
            var accessToken = tokenResponse.AccessToken;
            return accessToken;
        }

    }

4.Test从KeyVault获取秘密

static void Main(string[] args)
        {
            var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(KeyVaultBase.GetAccessToken));
            string secretUri = "https://KeyVaultNameSpace.vault.azure.net/secrets/secretsname/6341322679db4f3491814ff6178dee45";
            var sec = kv.GetSecretAsync(secretUri).Result;
        }

enter image description here