I want to get secret from a KeyVault, but the KeyVaultClient.AuthenticationCallback not called.
I created unittest that is MSTest, I have this code:
[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;
}
But it's never enter to the code inside the KeyVaultClient.AuthenticationCallback AuthenticationCallback.
What is the problem? The exception I am getting is:
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.TaskAwaiter
1.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.TaskAwaiter
1.GetResult() at d__18.MoveNext() in C:\MyProject\src\test\testValidation.cs:line 41
I have used exact same code and it is stepping in the AuthenticationCallBack function. Please check the screenshot.
Seems like Call back function throwing error because of the credential issue.
**at Microsoft.Azure.KeyVault.KeyVaultCredential.d__13.MoveNext()**
Please check the credential/certificate and try to debug your call back function.
Hope it helps.