我想在 winforms 应用程序中使用 azure key vault。我设法将应用程序添加到 Azure 活动目录,并使用 powershell 在密钥库中创建了一个秘密。该应用程序经过身份验证以使用该密钥。不幸的是,我无法在我的 winforms 应用程序中使用该秘密。我得到了例外:
*sts_token_request_failed:对安全令牌服务的令牌请求失败。检查 InnerException 以获取更多详细信息。
InnerException:AADSTS70002:验证凭据时出错。AADSTS50012:提供的客户端密码无效。*
配置:
<appSettings>
<add key="ClientId" value="<appId>" />
<add key="ClientSecret" value="<nameofsecret>" />
<add key="SecretUri" value="https://<vaultname>.vault.azure.net/secrets/<nameofsecret>" />
</appSettings>
我的代码是:
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;
}
}
我是否必须更改清单或 app.config 中的任何内容?什么是secretName?它是在 powershell 中创建的秘密的名称吗?在本机客户端应用程序中,我在活动目录中没有“配置”选项卡来添加密钥。这可能是问题吗?