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

Kam M 31 Reputation points
2020-09-03T09:15:57.39+00:00

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 methods 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.

The secret key date expiration is not set to expire in Azure portal.

Could you please advice?

Kind regards,

Kamal

Azure Key Vault
Azure Key Vault
An Azure service that is used to manage and protect cryptographic keys and other secrets used by cloud apps and services.
1,309 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.