locked
Retrieve Secret Update Date using C# RRS feed

  • Question

  • Hi,

    I am trying to retrieve the update date of a secret stored in Key Vault using C#.

    The reason I am doing this is I want to check that date and want to update the API access token if the update date is (today's date - 15)

    Thanks,

    Pratik


    Wednesday, May 20, 2020 3:37 PM

All replies

  • You can use Key Vault Secret Attributes to fetch the created, updated dates etc. of a Key Vault Secret using Azure Key Vault SDK in C#. For Example - 

    var keyvaultSecret = await kvClient.GetSecretAsync($"{kvURL}", secretName).ConfigureAwait(false);
    var updatedDate = keyvaultSecret.Attributes.Updated;
    Wednesday, May 20, 2020 8:42 PM
  • I am not able to get the update date, I'll explain the process I am following:

    I am having two classes:

    1. KeyVault.cs --> This class should have all the methods pertaining to key vault like GetSecret, WriteSecret, GetUpdateDateforSecret and so on.

    2. Authentication.cs --> This class will have the GetToken method which is of type string, inside this method I need to first check whether the secret's update date is (today's date - 15), if yes then create new secret (API access token here), If not then retrieve the secret value. Also, when I regenerate the secret I want o just create another version of the secret with an expiry time of 15 days, I also need help in this scenario as well.

    Thanks,

    Pratik

    Thursday, May 21, 2020 5:23 PM
  • Can you please provide Key Vault specific code snippet over here so that I can help you better.

    Thursday, May 21, 2020 5:35 PM
  • Below is the code snippet for retrieving the Secret Update date, this method will be called in Authentication class which is of type String and returns access token to the API call.

    public async Task<object> RetrieveSecretUpdateDate(string keyVaultUrl, string secretName)
            {
                var token = await _kvClient.GetSecretAsync(keyVaultUrl, secretName).ConfigureAwait(false);
                return Convert.ToDateTime(token.Attributes.Updated);
            }

    When I try to add the break-point to the above method at var token, then it jumps to the API response instead of providing the result.

    Is it because the Method is of type async? As you said in the first comment, I should use await, then async is also must, and the Authentication class is a normal sync one, is that the issue or it is something else?

    Thanks,

    Pratik

    Friday, May 22, 2020 4:23 PM