0

VaultSharp 包似乎包含了我想要的所有内容,并且有据可查。我尝试使用 VaultSharp 包从 Vault 服务器读取我们的秘密。但是我生锈的 C# 在Secret<SecretData> kv2Secret = await vaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync("/secret/my_corp/my_app/dev/db_creds");下面的“”行阻止了我,并显示错误消息:

'await' 运算符只能在异步方法中使用。考虑使用“异步”修饰符标记此方法并将其返回类型更改为“任务”。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VaultSharp;
using VaultSharp.V1.AuthMethods.AppRole;
using VaultSharp.V1.AuthMethods;
using VaultSharp.V1.Commons;
using VaultSharp.V1.AuthMethods.Token;

namespace VaultConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var vaultUrl = "https://vault-server.url.com:443";
            Program.by_token(vaultUrl);

        }

     
       static  void by_token(string vaultUrl)
        {
            // Initialize one of the several auth methods.
            IAuthMethodInfo authMethod = new TokenAuthMethodInfo("s.R2gFHDiup5wCeHHksfc2zKUN");

            // Initialize settings. You can also set proxies, custom delegates etc. here.
            var vaultClientSettings = new VaultClientSettings(vaultUrl, authMethod);

            IVaultClient vaultClient = new VaultClient(vaultClientSettings);

            // Use client to read a key-value secret.
           Secret<SecretData> kv2Secret = await vaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync("/secret/my_corp/my_app/dev/db_creds");
        }

    }
}

代码基本上是从http://rajanadar.github.io/VaultSharp/复制的

4

2 回答 2

2

该错误准确地告诉您您需要知道的内容。虽然让我们推断:

'await' 运算符只能在异步方法中使用。考虑使用 'async' 修饰符标记此方法并将其返回类型更改为 'Task'

static async Task Main(string[] args)
{
    var vaultUrl = "https://vault-server.url.com:443";
    await Program.by_token(vaultUrl);
}

 
static async Task by_token(string vaultUrl)
{
    ...
    Secret<SecretData> kv2Secret = await vaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync("/secret/my_corp/my_app/dev/db_creds");
}

这时候你应该对async 和 await模式做一些研究:

于 2020-07-15T00:18:45.397 回答
0

除了使方法异步之外,您还可以删除“等待”,这将使其同步而不是异步。

var kv2Secret = vaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync("/secret/my_corp/my_app/dev/db_creds");

为您做的最好的事情取决于您的用例,所以我建议按照其他答案中的建议先阅读它。

于 2020-11-10T09:18:06.173 回答