尝试从 Azure Key Vault 获取机密时获得未授权
Getting Unathorized when trying to get a secret from Azure key Vault
我正在使用 Microsoft.Azure.keyVault
尝试从 Azure 的密钥保管库获取机密。
我已将应用程序注册为本机和 Web API。
登录 AD 成功(可以通过在 AuthenticationContext.AcquireTokenAsync
上获取有效的 AccessToken
来确认这一点)。
在 Azure AD 中,两个应用程序都在 Key Vault 中获得了访问控制 (IAM) 和访问策略。我已经确认 Key Vault Base Url 和 Secret Name 是正确的,但是在进行以下调用时
var sec = kv.GetSecretAsync("https://xxxxxxx.vault.azure.net", "xxsecretnamexx").GetAwaiter().GetResult();
我一直收到错误消息
{"Operation returned an invalid status code 'Unauthorized'"} Microsoft.Azure.KeyVault.Models.KeyVaultErrorException
旁注:尝试通过以用户身份登录来执行此操作。获取token的代码如下
.AcquireTokenAsync(resourceUri,clientId, new Uri(redirectUri), new PlatformParameters(PromptBehavior.SelectAccount))
我们有工作代码使用已注册的 Azure AD 应用程序的 ID 和机密,该应用程序有权访问密钥保管库。
尝试在不使用关联的已注册 Azure AD 应用程序的 ID 和机密的情况下执行相同的操作,而是在获取 Azure AD 的访问令牌时显示登录提示。
我用下面的代码测试它,它在我这边工作正常。 resourceUri 是 https://vault.azure.net
static string appId = "application Id";
static string tenantId = "tenant id";
static string uri = "http://localhost:13526"; //redirect uri
static void Main(string[] args)
{
var kv = new KeyVaultClient(GetAccessToken);
var scret = kv.GetSecretAsync("https://xxxx.vault.azure.net", "xxxx").GetAwaiter().GetResult();
}
public static async Task<string> GetAccessToken(string azureTenantId,string clientId,string redirectUri)
{
var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
var tokenResult = await context.AcquireTokenAsync("https://vault.azure.net", appId, new Uri(uri), new PlatformParameters(PromptBehavior.SelectAccount));
return tokenResult.AccessToken;
}
以下是我的详细步骤
1.Registry 本机应用程序
2.Add KeyVault 权限
3.Add上面的代码在我这边测试一下
Packages.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Hyak.Common" version="1.0.2" targetFramework="net461" />
<package id="Microsoft.Azure.Common" version="2.0.4" targetFramework="net461" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net461" />
<package id="Microsoft.Azure.KeyVault" version="1.0.0" targetFramework="net461" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net461" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net461" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net461" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.17.0" targetFramework="net461" />
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net461" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net461" />
</packages>
我正在使用 Microsoft.Azure.keyVault
尝试从 Azure 的密钥保管库获取机密。
我已将应用程序注册为本机和 Web API。
登录 AD 成功(可以通过在 AuthenticationContext.AcquireTokenAsync
上获取有效的 AccessToken
来确认这一点)。
在 Azure AD 中,两个应用程序都在 Key Vault 中获得了访问控制 (IAM) 和访问策略。我已经确认 Key Vault Base Url 和 Secret Name 是正确的,但是在进行以下调用时
var sec = kv.GetSecretAsync("https://xxxxxxx.vault.azure.net", "xxsecretnamexx").GetAwaiter().GetResult();
我一直收到错误消息
{"Operation returned an invalid status code 'Unauthorized'"} Microsoft.Azure.KeyVault.Models.KeyVaultErrorException
旁注:尝试通过以用户身份登录来执行此操作。获取token的代码如下
.AcquireTokenAsync(resourceUri,clientId, new Uri(redirectUri), new PlatformParameters(PromptBehavior.SelectAccount))
我们有工作代码使用已注册的 Azure AD 应用程序的 ID 和机密,该应用程序有权访问密钥保管库。
尝试在不使用关联的已注册 Azure AD 应用程序的 ID 和机密的情况下执行相同的操作,而是在获取 Azure AD 的访问令牌时显示登录提示。
我用下面的代码测试它,它在我这边工作正常。 resourceUri 是 https://vault.azure.net
static string appId = "application Id";
static string tenantId = "tenant id";
static string uri = "http://localhost:13526"; //redirect uri
static void Main(string[] args)
{
var kv = new KeyVaultClient(GetAccessToken);
var scret = kv.GetSecretAsync("https://xxxx.vault.azure.net", "xxxx").GetAwaiter().GetResult();
}
public static async Task<string> GetAccessToken(string azureTenantId,string clientId,string redirectUri)
{
var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
var tokenResult = await context.AcquireTokenAsync("https://vault.azure.net", appId, new Uri(uri), new PlatformParameters(PromptBehavior.SelectAccount));
return tokenResult.AccessToken;
}
以下是我的详细步骤
1.Registry 本机应用程序
2.Add KeyVault 权限
3.Add上面的代码在我这边测试一下
Packages.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Hyak.Common" version="1.0.2" targetFramework="net461" />
<package id="Microsoft.Azure.Common" version="2.0.4" targetFramework="net461" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net461" />
<package id="Microsoft.Azure.KeyVault" version="1.0.0" targetFramework="net461" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net461" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net461" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net461" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.17.0" targetFramework="net461" />
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net461" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net461" />
</packages>