locked
Getting azure secret key return either null, Not Found, Object not set RRS feed

  • Question

  • Hi,

    I was able to get the secret key in my mobile app last month. However, it has stopped working. I tried all different solutions that I found online, and each returns different result.

    Here is the code:

     
    var keyClient = new KeyVaultClient(async (authority, resource, scope) =>
                    {
                        var adCredential = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(CLIENTID, CLIENTSECRET);
                        var authenticationContext = new AuthenticationContext(authority, null);
                        return (await authenticationContext.AcquireTokenAsync(resource, adCredential)).AccessToken;
    
                        });
                       var secret = keyClient.GetSecretAsync(BaseURL, key, SecretKeyVerstion);
                       
                    return secret.Result.Value.ToString();

    This is one of the method that I tried. I also tried the following code that was working before:

     public  void  GetAzureUrl()
            {
                try
                {
                    
                    if (string.IsNullOrEmpty(LocalDBStorage.AzureUrls))
                    {
                       
                        
                          keyVClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken));
                        var secret = await keyVClient.GetSecretAsync(SECRETURI);
                      LocalDBStorage.AzureUrls = GetSecretAsync(SECRETURI);
                       
                       keyVClient.Dispose();
                    }
                }
                catch (Exception ex)
                { throw ex; }
               
            }
    
     public string GetSecretAsync(string key)
            {
                if (Cache.TryGetValue(key, out var value))
                    return value;
                else
                {
    
                       var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetAccessTokenAsync));
    
                     var secret = (kv.GetSecretAsync(App.SECRETBase)).GetAwaiter().GetResult().Value;
    
    
                   
                    return secret.Result.Value.ToString();
                }
            }
    
      private async Task<string> GetAccessTokenAsync(string authority, string resource, string scope)
            {
               
    
                var clientId = CLIENTID;
                var clientSecret = CLIENTSECRET;
                ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);
    
                var context = new AuthenticationContext(authority, TokenCache.DefaultShared);
                var result = await context.AcquireTokenAsync(resource, clientCredential);
    
                return result.AccessToken;
    
              
            }
    

    I get the same results.

    Could you please advice?

    Kind regards,

    Kamal

    • Moved by CoolDadTx Wednesday, September 2, 2020 3:38 PM Azure related
    Monday, August 31, 2020 6:12 PM

All replies

  • Hi Kamal3,

    Thank you for posting here.

    Since your code worked normally before, and the new code is consistent with the previous code, then I think the code should be no problem.

    Is it possible that your secret key has expired?

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 1, 2020 6:12 AM
  • Questions related to using Azure should be posted in the Microsoft Q&A forums for Azure.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, September 1, 2020 2:07 PM
  • Hi Timon,

    I haven't thought of that. I will check to see if the secret key expired or not. If it has been expired, can I set the expiration to false ?

    Kind Regards,

    Kamal

    Tuesday, September 1, 2020 4:00 PM
  • Hi Michael,

    Sorry for the confusion. Can I move it to Azure forums?Regards,

    Kamal

    Tuesday, September 1, 2020 4:09 PM
  • I checked the secret key and Expiry date hasn't been set up.
    Tuesday, September 1, 2020 4:10 PM
  • This is one stack trace information when the code returns (Not Found)

    at Microsoft.Azure.KeyVault.KeyVaultClient.GetSecretWithHttpMessagesAsync (System.String vaultBaseUrl, System.String secretName, System.String secretVersion, System.Collections.Generic.Dictionary`2[TKey,TValue] customHeaders, System.Threading.CancellationToken cancellationToken) [0x005ff] in <6ea39e55cc3d4eb2bd582641f96dc85f>:0 at Microsoft.Azure.KeyVault.KeyVaultClientExtensions.GetSecretAsync (Microsoft.Azure.KeyVault.IKeyVaultClient operations, System.String vaultBaseUrl, System.String secretName, System.String secretVersion, System.Threading.CancellationToken cancellationToken)
    Tuesday, September 1, 2020 4:18 PM
  • Hi kamal3,

    The following link provides two methods for setting the expiration time of the secret key.

    Set Azure Secret Key Expiration

    If it still doesn't help, please follow CoolDadTx's advice and go to Microsoft Q&A to ask this question. The engineers there should be more familiar with Azure.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 2, 2020 8:24 AM
  • Thank you Timon. I will do as you instructed.
    Wednesday, September 2, 2020 1:19 PM
  • I cannot find Azure Forum category in the list when I want to ask a questions. I went through each categories.
    Wednesday, September 2, 2020 1:33 PM
  • We cannot move threads to other forums outside of MSDN. Please post your question over there manually. We'll move this to Off Topic.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, September 2, 2020 3:38 PM
  • Hi Kamal3,

    You can try this tag: azure-key-vault.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 3, 2020 1:19 AM