I want to use azure key vault in an winforms application. I managed to add the app to the Azure active directory and to create a secret in key vault with powershell. the app is authenticated to use the secret. Unfortunately I'm not able to use the secret in my winforms app. I get the exception:
*sts_token_request_failed: Token request to security token service failed. Check InnerException for more details.
InnerException: AADSTS70002: Error validating credentials. AADSTS50012: Invalid client secret is provided.*
config:
<appSettings>
<add key="ClientId" value="<appId>" />
<add key="ClientSecret" value="<nameofsecret>" />
<add key="SecretUri" value="https://<vaultname>.vault.azure.net/secrets/<nameofsecret>" />
</appSettings>
My code is:
public static string GetSecret()
{
var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken));
var sec = kv.GetSecretAsync(ConfigurationManager.AppSettings["SecretUri"]).Result.Value;
return sec;
}
public async static Task<string> GetToken(string authority, string resource, string scope)
{
var authContext = new AuthenticationContext(authority,true);
ClientCredential clientCred = new ClientCredential(ConfigurationManager.AppSettings["ClientId"],
ConfigurationManager.AppSettings["ClientSecret"]);
try
{
AuthenticationResult result = authContext.AcquireToken(resource, clientCred);
if (result == null)
throw new InvalidOperationException("Failed to obtain the JWT token");
return result.AccessToken;
}
catch (Exception ex)
{
return String.Empty;
}
}
Do I have to change anything in the manifest or in the app.config? What is the secretName? Is it the name of the secret created in powershell? In native client application I have no tab "configure" in the active directory to add an key. Might this be the problem?