@azure/identity node.js 无法进行身份验证

时间:2021-01-27 14:55:49

标签: azure azure-active-directory azure-storage

我一直在关注我可以在互联网上找到的每一个示例,以使我能够使用应用程序身份通过 js/node 通过 azure 进行身份验证,如下例所示:

        const account = process.env.ACCOUNT_NAME || '';

        // Azure AD Credential information is required to run this sample:
        if (
          !process.env.AZURE_TENANT_ID ||
          !process.env.AZURE_CLIENT_ID ||
          !process.env.AZURE_CLIENT_SECRET
        ) {
          console.warn(
            'Azure AD authentication information not provided, but it is required to run this sample. Exiting.'
          );
          return {
            success: false,
            message:
              'Azure AD authentication information not provided, but it is required to run this sample. Exiting.',
          };
        }
        const defaultAzureCredential = new DefaultAzureCredential();

        console.log('credential', defaultAzureCredential);

我的代码中包含所有环境变量,并且我已经检查、双重检查和三次检查这些都是准确的。

尝试运行代码时,我在 console.log 中收到此错误:

credential DefaultAzureCredential {
  UnavailableMessage: 'DefaultAzureCredential => failed to retrieve a token from the included credentials',
  _sources: [
    EnvironmentCredential { _credential: [ClientSecretCredential] },
    ManagedIdentityCredential {
      isEndpointUnavailable: null,
      identityClient: [IdentityClient]
    },
    ManagedIdentityCredential {
      isEndpointUnavailable: null,
      clientId: '04e6dd8e-0000-0000-0000-eb9b3eb60e27',
      identityClient: [IdentityClient]
    },
    AzureCliCredential {},
    VisualStudioCodeCredential {
      cloudName: 'AzureCloud',
      identityClient: [IdentityClient],
      tenantId: 'common'
    }
  ]
}

我现在完全卡住了。由于需要连接到多个存储帐户(甚至使用这些凭据创建新的存储帐户),我不想使用共享访问令牌。

非常欢迎任何建议、调试或建议....

1 个答案:

答案 0 :(得分:0)

DefaultAzureCredential 在您的问题中有效,即使它显示了不可用的消息。

您可以控制 EnvironmentCredential,它将包含环境变量。

enter image description here

注意:如果您只是使用环境变量,我建议您使用 EnvironmentCredential。

<块引用>

DefaultAzureCredential 和 EnvironmentCredential 可以配置 带环境变量。

使用 DefaultAzureCredential 在 Key Vault 中获取机密:

enter image description here