如何使用客户端ID和密钥进行Azure密钥保管库身份验证?

huangapple go评论60阅读模式
英文:

how to authenticate azure key vault using client id and secret?

问题

我有一个 Azure 密钥保管库,并尝试从该保管库中获取密钥并在 Node.js 控制台中打印出来。这是我从文档中获得的代码:

const { DefaultAzureCredential} = require("@azure/identity");
const { SecretClient } = require("@azure/keyvault-secrets");

const credential = new DefaultAzureCredential();

// Build the URL to reach your key vault
const vaultName = "web-designer";
const url = `https://${vaultName}.vault.azure.net`;

// Lastly, create our secrets client and connect to the service
const client = new SecretClient(url, credential);

const secretName = "web-designer-secret";

client.getSecret(secretName).then((res)=>{
    console.log(res);
}).catch((err)=>{
    console.log("error",err);
})

在这里,他们使用 DefaultAzureCredential 来验证帐户。但我想要使用客户端 ID 和客户端密钥来验证我的密钥保管库,而不是使用 DefaultAzureCredential。如何在不使用 DefaultAzureCredential 的情况下执行此操作?

我尝试使用 ManagedIdentityCredential,但不起作用。

英文:

I have a azure key vault and i am trying to get the secret from that vault and print it in console in node js. This is the code i got from documentation,

const { DefaultAzureCredential} = require("@azure/identity");
const { SecretClient } = require("@azure/keyvault-secrets");

const credential = new DefaultAzureCredential();

// Build the URL to reach your key vault
const vaultName = "web-designer";
const url = `https://${vaultName}.vault.azure.net`;

// Lastly, create our secrets client and connect to the service
const client = new SecretClient(url, credential);

const secretName = "web-designer-secret";

client.getSecret(secretName).then((res)=>{
    console.log(res);
}).catch((err)=>{
    console.log("error",err);
})

Here they are using DefaultAzurecredential to validate account. But instead of that i want to use client id and client secret to validate my key vault. How to do this without DefaultAzureCredential?

I tried ManagedIdentityCredential but not working.

答案1

得分: 1

以下是已翻译的内容:

在我这边重新生成后,我成功使用以下代码实现了使用 ClientSecretCredential 进行身份验证。

const { ClientSecretCredential } = require("@azure/identity");
const { SecretClient } = require("@azure/keyvault-secrets");

const credential = new ClientSecretCredential("<TENANTID>","<CLIENTID>","<CLIENTSECRET>");

const vaultName = "<VAULTNAME>";
const url = `https://${vaultName}.vault.azure.net`;

const client = new SecretClient(url, credential);

const secretName = "<SECRETNAME>";

client.getSecret(secretName).then((res)=>{
    console.log(res);
}).catch((err)=>{
    console.log("error",err);
})

RESULTS:

如何使用客户端ID和密钥进行Azure密钥保管库身份验证?

英文:

After reproducing from my end, I could able to achieve Authentication using ClientSecretCredential following the below code.

const { ClientSecretCredential } = require(&quot;@azure/identity&quot;);
const { SecretClient } = require(&quot;@azure/keyvault-secrets&quot;);

const credential = new ClientSecretCredential(&quot;&lt;TENANTID&gt;&quot;,&quot;&lt;CLIENTID&gt;&quot;,&quot;&lt;CLIENTSECRET&gt;&quot;);

const vaultName = &quot;&lt;VAULTNAME&gt;&quot;;
const url = `https://${vaultName}.vault.azure.net`;

const client = new SecretClient(url, credential);

const secretName = &quot;&lt;SECRETNAME&gt;&quot;;

client.getSecret(secretName).then((res)=&gt;{
    console.log(res);
}).catch((err)=&gt;{
    console.log(&quot;error&quot;,err);
})

RESULTS:

如何使用客户端ID和密钥进行Azure密钥保管库身份验证?

huangapple
  • 本文由 发表于 2023年1月10日 13:53:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75065979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定