我正在 Azure 上工作。我有一个访问 Azure Key Vault 的 Windows 服务。
我的代码看起来像这样:
public static async Task<string> GetToken(string authority, string resource, string scope)
{
var authContext = new AuthenticationContext(authority);
ClientCredential clientCred = new ClientCredential(...); //app id, app secret
AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);
if (result == null)
throw new InvalidOperationException("Failed to obtain the JWT token");
return result.AccessToken;
}
public static string GetSecret(string secretName)
{
KeyVaultClient keyVaultClient = new KeyVaultClient(GetToken);
try
{
return keyVaultClient.GetSecretAsync("my-key-vault-url", secretName).Result.Value;
}
catch(Exception ex)
{
return "Error";
}
}
构建并部署 Windows 服务后,我启动了它。然后我得到这个例外:
客户端地址(IP地址)未经授权且调用者不是受信任的服务
但是,我可以远程登录到密钥库:
telnet projectName-keyvault 443
我已经搜索过这个问题,但找不到任何解决方案。接下来我可以尝试什么?
该错误正确地表明您的客户端IP地址未经授权。
如果您启用了该设置,则需要在 Azure keyvault 中添加客户端 IP。
@Nancy Xiong - MSFT 评论的是我的密钥保管库的问题。
在密钥保管库的防火墙和虚拟网络中,我添加了访问密钥保管库的IP地址。它解决了我的问题。