c# - 缺少将 Microsoft.IdentityModel.Clients.ActiveDirectory 3.13.9 与 Azure KeyVault C# SDK 结合使用的程序集引用

标签 c# .net azure nuget azure-keyvault

出于某种原因,我必须最新的 Microsoft.IdentityModel.Clients.ActiveDirectory v3.13.9Microsoft.Azure.KeyVault v2.0.6 。它总是会因缺少程序集引用而引发异常,但将 Microsoft.IdentityModel.Clients.ActiveDirectory 降级到 v2.28.4(我不允许使用该版本)可以解决此问题 known issue .

异常(exception)情况如下:

  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);时抛出异常。

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

最佳答案

在我的例子中,引用仅安装到类库项目,而不是安装到执行程序集,因此它抛出此异常.

Microsoft.IdentityModel.Clients.ActiveDirectory nuget 包添加到执行程序集对我有用。

希望对你有帮助

关于c# - 缺少将 Microsoft.IdentityModel.Clients.ActiveDirectory 3.13.9 与 Azure KeyVault C# SDK 结合使用的程序集引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44075581/

相关文章:

c# - 意外的十进制值行为

c# - 重写文本时不保留非英文字符

.net - 有没有一种简单的方法可以从文本文件生成代码?

asp.net - 如何使用 Windows 集成安全 : Elmah ignores my settings 在 ASP.Net MVC 4 上保护 Elmah

azure - azure 队列触发器中的单例未按预期工作

c# - 通过代码将 Eventhandler 添加到 DataTemplate 内的形状

c# - 快速调用 WCF 服务时出错

c# - 编译器是否优化对 const 变量和文字 const 数字的操作?

azure - 通过分区和行键对列表查询Azure表存储

powershell - 在 Powershell 中通过镜像在 Azure 中创建 Linux 虚拟机