Azure 仍在使用我的旧租户 ID 进行身份验证

问题描述 投票:0回答:1

我一直在使用我的 Azure 微软帐户,并使用其凭据成功进行了身份验证。我还在该帐户中创建了一个 Entra 应用程序并为其设置了权限。

最近,我使用不同的电子邮件创建了一个新的 Azure 帐户,并在那里设置了一个新的 Entra 应用程序。我想完全切换到这个新的 Azure 帐户。

在我的 Python 应用程序中,我使用带有 DefaultCredentials() 的 Azure SDK,并设置以下环境变量以指向新帐户:

  • AZURE_TENANT_ID
  • AZURE_CLIENT_ID
  • AZURE_CLIENT_SECRET

当我使用旧帐户的凭据时,一切正常,并且我可以将 blob 上传到我的存储帐户。但是,当我通过更新环境变量切换到新帐户时,Azure 仍然尝试使用旧的 AZURE_TENANT_ID,导致授权错误:

azure.core.exceptions.ClientAuthenticationError:当前凭据未配置为获取租户 {my_old_tenant_id} 的令牌。要启用此租户获取令牌,请在创建凭证时将其添加到additional_allowed_tenants,或将“*”添加到additional_allowed_tenants以允许为任何租户获取令牌。

尝试过:

  1. 从我的代码中完全删除我的旧tenant_id。
  2. 删除了 ~/.azure 文件夹。
  3. 在天蓝色代码中硬编码我的新tenant_id。
python azure azure-python-sdk
1个回答
0
投票

如果请求的

ClientAuthenticationError
与凭证上配置的租户 ID 不匹配,则会出现
tenant_id

运行命令

az account show
并检查有效租户当前是否处于活动状态。

enter image description here

将应从中获取令牌的所有租户 ID 添加到凭证选项中的

additionally_allowed_tenants
列表,如博客MSDOC中所述。

credential  =  DefaultAzureCredential(additionally_allowed_tenants=["*"])

(或)

credential = DefaultAzureCredential(additionally_allowed_tenants = ["<tenant_id_1>", "<tenant_id_2>"])

示例代码:

from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient

account_url = "https://storageaccountname.blob.core.windows.net"
container_name = "container_name"
blob_name = "<blob_name>"

download_file_path = "<file_location_to_download>"

credential = DefaultAzureCredential(additionally_allowed_tenants=["*"])
blob_service_client = BlobServiceClient(account_url=account_url, credential=credential)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)

with open(download_file_path, "wb") as download_file:
    download_file.write(blob_client.download_blob().readall())

print(f"Blob '{blob_name}' downloaded to '{download_file_path}' successfully.")

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.