客户端地址未经授权且调用方不是 Azure 中的受信任服务

Client address is not authorized and caller is not a trusted service in Azure

我在 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 服务后,我启动了它。然后我得到这个例外:

Client address (IPaddress) is not authorized and caller is not a trusted service

但是,我可以远程登录到密钥库:

telnet projectName-keyvault 443

我搜索过这个问题,但找不到任何solution.Any这方面的帮助将非常有帮助。

我试过你的代码,我能够从密钥库中获取数据。

@Nancy Xiong - MSFT 评论说我的密钥库有问题。

在密钥保管库的防火墙和虚拟网络中,我添加了访问密钥的 IP 地址vault.It解决了我的问题。

该错误正确显示您的客户端 IP 地址未被授权。

如果您启用了该设置,则需要在 Azure keyvault 中添加客户端 IP。

进一步阅读: