出于某种原因,我必须最新的 Microsoft.IdentityModel.Clients.ActiveDirectory
v3.13.9与 Microsoft.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/