访问Azure KeyVault时出现“找不到方法”错误

时间:2018-02-27 21:09:23

标签: azure-keyvault

我正在尝试从我的.Net Web应用程序的application_start事件中获取Azure Keyvault的连接字符串。

    protected void Application_Start(Object sender, EventArgs e)
    {
        var azureServiceTokenProvider = new AzureServiceTokenProvider();
                var kvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken));

      RegisterRoutes(RouteTable.Routes);
   }

 public static async Task<string> GetToken(string authority, string resource, string scope)
    {
        var authContext = new AuthenticationContext(authority);
        ClientCredential clientCred = new ClientCredential(ConfigurationManager.AppSettings["clientId"],
                                                  ConfigurationManager.AppSettings["clientSecret"]);
                AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);

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

      return result.AccessToken;
  }

但我在下面收到此错误。 这是一个带有.Net 4.6的新MVC Web应用程序。 完全相同的代码在Console应用程序上正常工作。 我比较了包版本,它们是相同的。 我也尝试将该代码放在控制器的构造函数中但是同样的错误。 有什么想法吗?

  

未找到方法:'无效   Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback,   System.Net.Http.DelegatingHandler [])”

     

[MissingMethodException:找不到方法:'无效   Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback,   System.Net.Http.DelegatingHandler [])”。]
  WebApplication1.MvcApplication.Application_Start()in   C:\ Users \ test \ Documents \ Visual Studio   2017 \项目\ ConsoleApp1 \ WebApplication1 \ Global.asax.cs中:27

     

[HttpException(0x80004005):找不到方法:'无效   Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback,   System.Net.Http.DelegatingHandler [])”。]
  System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext的   上下文,HttpApplication app)+10062145
  System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr的   appContext,HttpContext上下文,MethodInfo []处理程序)+118
  System.Web.HttpApplication.InitSpecial(HttpApplicationState状态,   MethodInfo []处理程序,IntPtr appContext,HttpContext context)+173
  System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr的   appContext,HttpContext context)+336
  System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr的   appContext)+296

     

[HttpException(0x80004005):找不到方法:'无效   Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback,   System.Net.Http.DelegatingHandler [])”。]
  System.Web.HttpRuntime.FirstRequestInit(HttpContext context)+10043436   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)+95   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest   wr,HttpContext context)+254

<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net461" />
  <package id="bootstrap" version="3.0.0" targetFramework="net461" />
  <package id="jQuery" version="1.10.2" targetFramework="net461" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.0.6" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.Web" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.WindowsServer" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net461" />
  <package id="Microsoft.Azure.KeyVault" version="2.3.2" targetFramework="net461" />
  <package id="Microsoft.Azure.KeyVault.WebKey" version="2.0.7" targetFramework="net461" />
  <package id="Microsoft.Azure.Services.AppAuthentication" version="1.1.0-preview" targetFramework="net461" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.7" targetFramework="net461" />
  <package id="Microsoft.Extensions.Configuration" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.Configuration.Abstractions" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.Configuration.AzureKeyVault" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.Configuration.FileExtensions" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.FileProviders.Abstractions" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.FileProviders.Physical" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.FileSystemGlobbing" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.Primitives" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.14.2" targetFramework="net461" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net461" />
  <package id="Microsoft.Net.Compilers" version="2.1.0" targetFramework="net461" developmentDependency="true" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.8" targetFramework="net461" />
  <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.7" targetFramework="net461" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
  <package id="Modernizr" version="2.6.2" targetFramework="net461" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net461" />
  <package id="Respond" version="1.2.0" targetFramework="net461" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="4.4.0" targetFramework="net461" />
  <package id="WebGrease" version="1.5.2" targetFramework="net461" />
</packages>

3 个答案:

答案 0 :(得分:1)

我不得不从引用中删除System.Net.Http并重新添加它,因为应用程序因某种未知原因加载了该库的2个不同版本。

答案 1 :(得分:1)

在我们的案例中,我们根据KeyVault nuget软件包在项目中使用以下属性来解决该问题:

<ImplicitlyExpandNETStandardFacades>false</ImplicitlyExpandNETStandardFacades>

结果,这会强制引用GAC中的System.Net.Http。

答案 2 :(得分:1)

面临类似问题。我的解决方案是:

我在.NET Framework 4.6.2中具有解决方案,当我从块中添加KeyVault SDK时,它也采用了System.Net.Http 4.3.4。但是,根据密钥库SDK的描述,仅.NET标准1.4和2.0才需要,而.NET Framework则没有。当我的整个应用程序使用GAC的System.Net.Http 4.0.0时,我手动删除了通过块添加的System.Net.Http(将其从packages.config中删除),并将引用从GAC添加到了4.0.0版。

adding the reference from GAC

在那之后,我摆脱了错误。