KeyVaultClient.AuthenticationCallback不火

KeyVaultClient.AuthenticationCallback not fire

我想从 KeyVault 中获取秘密,但是 KeyVaultClient.AuthenticationCallback 没有被调用。

我创建了单元测试,即 MSTest,我有这段代码:

 [TestInitialize]
    public void SetupTest()
    {
        CreateKeyvalut();
    }

    public async void CreateKeyvalut()
    {
        try
        {
            IKeyVaultClient keyVaultClient = GetKeyVaultClient(_clientId, _certificateThumbprint);

            var password = await GetSecretValueAsync(_secretIdentifier, keyVaultClient);
        }
        catch (Exception ex)
        {
            string errorMessage = $"[KeyVault] Error occurred when trying to connect Key Vault. Exception: {ex}";
            Trace.TraceWarning(errorMessage);

            throw;
        }
    }
    public static IKeyVaultClient GetKeyVaultClient(string clientId, string certificateThumbprint) {
        return new KeyVaultClient(AuthenticationCallback(clientId, certificateThumbprint));
    }

    public static KeyVaultClient.AuthenticationCallback AuthenticationCallback(string clientId, string certificateThumbprint)
    {
        return async (authority, resource, scope) =>
        {
            X509Certificate2 certificate = GetCertificate(certificateThumbprint);
            var context = new AuthenticationContext(authority);
            var clientCredentials = new ClientAssertionCertificate(clientId, certificate);
            AuthenticationResult result = await context.AcquireTokenAsync(resource, clientCredentials).ConfigureAwait(false);
            return result.AccessToken;
        };
    }

    public static async Task<string> GetSecretValueAsync(string secretIdentifier, IKeyVaultClient keyVaultClient)
    {
        var secretTask = await keyVaultClient.GetSecretAsync(secretIdentifier);
        return secretTask.Value;
    }

但它永远不会进入 KeyVaultClient.AuthenticationCallback AuthenticationCallback 中的代码。

问题是什么? 我得到的例外是:

The thread 0x492c has exited with code 0 (0x0). testhost.exe Warning: 0 : [KeyVault] Error occurred when trying to connect Key Vault. Exception: System.Threading.ThreadAbortException: Thread was being aborted. at Microsoft.Rest.RetryDelegatingHandler.d__15.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.KeyVault.KeyVaultCredential.d__13.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.KeyVault.KeyVaultClient.d__65.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.KeyVault.KeyVaultClientExtensions.d__13.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at <GetSecretValueAsync>d__24.MoveNext() in --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at d__18.MoveNext() in C:\MyProject\src\test\testValidation.cs:line 41

我使用了完全相同的代码,它正在执行 AuthenticationCallBack 函数。请查看截图。

似乎由于凭据问题,回调函数抛出错误。

**at Microsoft.Azure.KeyVault.KeyVaultCredential.d__13.MoveNext()**

请检查 credential/certificate 并尝试调试您的回调函数。

希望对您有所帮助。