3

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

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

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

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

有人知道如何解决这个问题吗?谢谢。

问候, 阿尔文

4

2 回答 2

1

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

于 2017-03-05T06:58:30.277 回答
0

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

在此处输入图像描述 图 1. Microsoft.Azure.KeyVault 包的版本历史

现在错误消失了,无需在 project.json 中导入“net451”框架。

于 2017-03-06T07:48:48.540 回答