ASP.NET 使用 Azure KeyVault 的核心 - 委派 KeyVaultClient.AuthenticationCallback 时出错

ASP.NET Core using Azure KeyVault - Error on delegating the KeyVaultClient.AuthenticationCallback

我想将我在 ASP.NET Core 1.0 上的项目与 Microsoft Azure KeyVault 集成。但似乎 Microsoft.Azure.KeyVault 包与 "netcoreapp1.0" 框架不兼容(我尝试使用 NuGet 包管理器下载包,但显示 "incompatible package" 错误消息)。因此,我在此块中显示的 project.json 中导入 "net451" 框架:

"frameworks": {
"netcoreapp1.0": {
  "imports": [
    "net451",
    "dotnet5.6",
    "portable-net45+win8"
    ]
  }  
},

导入"net451"框架后,错误消失了。现在我想启动一个新的 KeyVaultClient class 显示在这个块中:

public void GetKeyVaultSecret()
{
    var keyVaultClient = new KeyVaultClient(this.GetTokenAsync);
    // ....
}

private async Task<string> GetTokenAsync(string authority, string resource, string scope)
{
    var authenticationContext = new AuthenticationContext(authority);
    var authenticationResult =
        await authenticationContext.AcquireTokenAsync(resource, this.clientAssertionCertificate);
    return authenticationResult.AccessToken;
}

问题是我在 this.GetTokenAsync 上收到此错误消息,我已经搜索了数小时的解决方案但没有任何运气:Argument 1:cannot convert from 'method group' to 'KeyVaultClient.AuthenticationCallback'

如果我改变

var keyVaultClient = new KeyVaultClient(this.GetTokenAsync);

至:

var keyVaultClient = new KeyVaultClient((authority, resource, scope) => this.GetTokenAsync(authority, resource, string.Empty));

我仍然收到错误消息:Cannot convert lambda expression to type 'KeyVaultClient.AuthenticationCallback' because it is not a delegate type

有人知道怎么解决这个问题吗?谢谢。

此致, 阿尔文

错误 cannot convert from 'method group' 是因为您有名称为 GetTokenAsync 的重载或扩展(即不止一种方法)。尝试重命名其中之一,应该可以。

因此,在放弃这个问题很长时间之后,由于@fernacolo 的回答,我决定再次研究它。原来当时我用的是Microsoft.Azure.KeyVault包的1.0.0版本(当时是最新版本,如图1)。现在该软件包的 2.0.0 版可用,当我找到更新日志时,我看到了这个 post https://docs.microsoft.com/en-us/azure/key-vault/key-vault-dotnet2api-release-notes,其中指出“.NET Core 受 Azure Key Vault 2.0 版本的支持。NET/C# 库".

图 1. Microsoft.Azure.KeyVault 包

的版本历史

错误现已消失,无需在 project.json 中导入 "net451" 框架。