Azure Key Vault 401身份验证错误

时间:2018-10-08 12:05:52

标签: dependencies azure-application-insights http-status-code-401 azure-keyvault

我正在从我的azure无状态服务结构应用程序的密钥仓库中获取秘密值,并从100个密钥仓库秘密中仅获取2个密钥仓库秘密的401依赖项错误(如果我通过连接的应用程序见解进行检查)。 下面给出的是通过应用洞察显示的关键保险库密钥之一的相关性错误的屏幕截图。

image

此处的请求路径为https://mykeyvaultname.vault.azure.net:443/secrets/PushMessagingSecretsTopicName/?api-version=7.0

下面给出了我获取密钥库机密的代码-

    public async Task<string> GetSecretAsync(string secretName, string clientId, string appKey, string vaultAddress)
            {
                string secretValue = string.Empty;
                if (string.IsNullOrEmpty(secretName))
                    throw new ArgumentNullException(nameof(secretName));

                if (string.IsNullOrEmpty(clientId))
                    throw new ArgumentNullException(nameof(clientId));

                if (string.IsNullOrEmpty(appKey))
                    throw new ArgumentNullException(nameof(appKey));

                if (string.IsNullOrEmpty(vaultAddress))
                    throw new ArgumentNullException(nameof(vaultAddress));

                var secretIdentifier = vaultAddress + "secrets/" + secretName;
                string cacheKey = secretIdentifier + clientId + appKey;

                secretValue = await GetSecretValue(clientId, appKey, secretIdentifier, cacheKey);

                return secretValue;
            }

private async Task<string> GetSecretValue(string clientId, string appKey, string secretIdentifier, string cacheKey)
        {
            IAdAuthentication authToken = new AdAuthentication
            {
                ClientId = clientId,
                AppKey = appKey
            };
            KeyVaultClient keyVaultClient = new KeyVaultClient(authToken.GetAuthenticationTokenAsync);

            // Get secret from the KeyVault.

            SecretBundle secret = null;

            Task tskGetSecret = Task.Run(async () =>
            {
                        //Here I am getting exception with response
                        secret = await keyVaultClient.GetSecretAsync(secretIdentifier).ConfigureAwait(false);
            });
            await Task.WhenAny(tskGetSecret);

            if (tskGetSecret.IsFaulted || tskGetSecret.IsCanceled)
            {
                secret = null;
            }

            string secretValue = string.Empty;
            if (secret != null && secret.Value != null)
            {
                secretValue = secret.Value.Trim();
            }

            return secretValue;
        }

我进一步调试了该问题,以下是我的发现-

  1. 获取特定keyVaultSecret的值时发生异常。

  2. 除了例外,还成功获取了秘密值。

  3. 例外是:Microsoft.Rest.TransientFaultHandling.HttpRequestWithStatusException: 'Response status code indicates server error: 401 (Unauthorized).'

StackTrace:-

at Microsoft.Rest.RetryDelegatingHandler.<>c__DisplayClass11_0.<<SendAsync>b__1>d.MoveNext()

1 个答案:

答案 0 :(得分:0)

我正在关闭此问题,因为通过进一步调试,我发现该问题不是间歇性的,并且与特定的密钥库机密无关。在为应用程序获取第一个密钥保险库密钥的值时,总是会发生问题。我正在关闭此问题,并打开了一个new issue并提供了适当的详细信息。