【问题标题】:Azure KeyVault expiration date updateAzure KeyVault 到期日期更新
【发布时间】:2018-12-19 03:24:15
【问题描述】:

是否可以在不通过 Microsoft.Azure.KeyVault 包在 KeyVault 中创建新版本的密钥的情况下更新密钥的到期日期?我可以在 Azure 门户中执行此操作,但需要能够以编程方式执行此操作。

【问题讨论】:

    标签: c# azure azure-keyvault


    【解决方案1】:

    是的,可以在不创建新版本的情况下更新现有密钥的到期日期。

    这里是快速而肮脏的示例 C# 代码。仔细查看被调用的SecretAttributesclient.UpdateSecretAsync 方法。

    Expires 是您需要设置的秘密属性。

    我正在使用KeyVaultClientExtensions.UpdateSecretAsync Method

    using Microsoft.Azure.KeyVault;
    using Microsoft.Azure.KeyVault.Models;
    using Microsoft.IdentityModel.Clients.ActiveDirectory;
    using System;
    using System.IO;
    using System.Net.Http;
    using System.Threading.Tasks;
    
    namespace UpdateKeyVaultSecret
    {
        class Program
        {
            static void Main(string[] args)
            {
                UpdateSecretAttributes("https://rohitvault1.vault.azure.net/secrets/mysecret1").GetAwaiter().GetResult();
    
                Console.ReadLine();
            }
    
    
            private static async Task<string> GetAccessTokenAsync(string authority, string resource, string scope)
            {
                var authContext = new AuthenticationContext(authority);
                ClientCredential clientCred = new ClientCredential("<my-app-clientid>", "<my-app-client-secret>");
                AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);
    
                if (result == null)
                    throw new InvalidOperationException("Failed to obtain the JWT token");
    
                return result.AccessToken;
            }
    
            public static async Task<string> GetSecretFromVault(string secretKeyIdentifier)
            {
                var client = new KeyVaultClient(
                    new KeyVaultClient.AuthenticationCallback(GetAccessTokenAsync),
                    new System.Net.Http.HttpClient());
    
                var secret = await client.GetSecretAsync(secretKeyIdentifier).ConfigureAwait(false);
    
                return secret.Value;
            }
    
            public static async Task<string> UpdateSecretAttributes(string secretKeyIdentifier)
            {
                var client = new KeyVaultClient(
                    new KeyVaultClient.AuthenticationCallback(GetAccessTokenAsync),
                    new System.Net.Http.HttpClient());
    
                SecretAttributes attributes = new SecretAttributes();
            attributes.Expires = DateTime.UtcNow.AddDays(15);
    
                var secret = await client.UpdateSecretAsync(secretKeyIdentifier, null, attributes, null).ConfigureAwait(false);
    
                return secret.Value;
            }
        }
    }
    

    另外,还有其他程序化选项。我只是简单地提到这些,因为这个问题很笼统,有人可能也会在这里寻找 C# 以外的方法:

    【讨论】:

    • 原来,因为我已经在使用 UpdateSecretAsync 方法,它正在更新,只是 Azure 门户没有刷新值,即使我单击了提供的刷新按钮 - 最终显示页面重新加载该值已更新
    • @CraigM 这是有道理的。我在门户端也看到过这种情况。
    猜你喜欢
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多