列出KeyVault中的秘密而无需登录每个秘密?

时间:2017-03-07 16:49:25

标签: c# asp.net-mvc azure azure-keyvault

我已成功设法列出Azure KeyVault中的所有秘密 - 但是每次我想获得下一个秘密时,我都需要拨打电话获取令牌。

如何存储凭据,以便我只需在循环期间登录一次?

    public async Task<List<string>> getsecretslist(string url)
    {

        var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken));

        List<string> secretlist = new List<string>(); 
        var all = kv.GetSecretsAsync(url);
        var myId = "";
        foreach (Microsoft.Azure.KeyVault.Models.SecretItem someItem in all.Result)
        {
            myId = someItem.Id;
            var mOtherThing = someItem.Identifier;
            var yep = await kv.GetSecretAsync(mOtherThing.ToString());
            secretlist.Add(yep.Value);
        }

        return secretlist;
    }

4 个答案:

答案 0 :(得分:8)

在GetToken回调方法中,只要访问令牌有效且未过期,您就需要缓存它。然后,您的回调将返回缓存的访问令牌,而不是再次进行身份验证。以下代码段将使用ADAL默认令牌缓存(例如TokenCache.DefaultShared)。

public static async Task<string> GetToken(string authority, string resource, string scope)
{
    var assertionCert = new ClientAssertionCertificate(clientId, certificate);
    var context = new AuthenticationContext(authority, TokenCache.DefaultShared);
    var result = await context.AcquireTokenAsync(resource, assertionCert).ConfigureAwait(false);

    return result.AccessToken;
}

答案 1 :(得分:1)

我找到的最佳方法是保存您在GetToken函数中获得的令牌,例如:

var authenticationContext = new AuthenticationContext(authority, TokenCache.DefaultShared);

var authenticationResult = await authenticationContext.AcquireTokenAsync(resource, KeyVaultUserClientId, new Uri(KeyVaultRedirectUri), new PlatformParameters(PromptBehavior.SelectAccount)).ConfigureAwait(false);

return authenticationResult.AccessToken;

然后我只是改变了客户端的getter,所以它会检查到期时间,如果它仍然有效(应该有60分钟到期)它将返回一个更简单的客户端返回lastAuthenticationResult

private static KeyVaultClient KeyVaultClient
{
    get
    {
        if (lastAuthenticationResult != null && DateTime.UtcNow.AddSeconds(5) < lastAuthenticationResult.ExpiresOn)
        {
            if (m_cachedKeyVaultClient != null)
            {
                return m_cachedKeyVaultClient;
            }
            else
            {
                return new KeyVaultClient(getCachedToken);
            }
        }

        if (m_keyVaultClient == null)
            m_keyVaultClient = new KeyVaultClient(GetAccessTokenAsync);

        return m_keyVaultClient;
    }
}


private static async Task<string> getCachedToken(string authority, string resource, string scope)
{
    return lastAuthenticationResult.AccessToken;
}

答案 2 :(得分:0)

您无需在循环中调用GetSecretAsync。密码已经包含在通过调用GetSecretsAsync设置的结果中。这就是您重复验证的原因。

这是对您的循环的简单更改,以执行您要查找的内容。

var all = kv.GetSecretsAsync(url).GetAwaiter().GetResult();
foreach (var secret in all.Value)
{
    secretlist.Add(secret.Id);
}

答案 3 :(得分:0)

要列出Azure密钥保管箱中的所有机密,您需要使用方法GetSecretsAsync。 您需要提供两个秘密权限:Get和List。

enter image description here

下面是工作函数,它将返回秘密列表作为键值对:

 public async Task<List<KeyValuePair<string, string>>> ListAllSecrets()
    {
        try
        {
            var azureServiceTokenProvider = new AzureServiceTokenProvider();
            var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));

            var Secret = keyVaultClient.GetSecretsAsync
                ("https://xyz.vault.azure.net").GetAwaiter().GetResult();

            var dictionary = new List<KeyValuePair<string, string>>();
            foreach (var item in Secret)
            {
                var value = keyVaultClient.GetSecretAsync(item.Id).GetAwaiter().GetResult().Value;
                dictionary.Add(new KeyValuePair<string, string>(item.Identifier.Name, value));
            }

            return dictionary;
        }
        catch (Exception ex)
        {
            return default(List<KeyValuePair<string, string>>);
        }
    }