我正在尝试从以下位置连接到 Azure Key Vault 实例:.NET 4.7
在 IIS 和调试器下本地运行的应用程序 (Visual Studio 202217.4.4
)但遇到以下异常Azure.Identity
当调用执行 KeyVault 操作(例如)时尝试检索令牌以向 Azure 进行身份验证时包GetSecretAsync()
.
DefaultAzureCredential 无法从包含的内容中检索令牌
证书。有关详细信息,请参阅故障排除指南。https://aka.ms/azsdk/net/identity/defaultazurecredential/troubleshoot
- ManagedIdentityCredential 身份验证不可用。多次尝试未能从托管身份端点获取令牌。
- 无法在 C:\WINDOWS\system32\config\systemprofile\AppData\Local.IdentityService\AzureServiceAuth\tokenprovider.json 访问 Visual Studio 令牌提供程序
我需要在生产等云环境中通过用户分配的托管标识连接到 KeyVault 实例,而在开发环境中,我们因此需要通过开发人员的 Visual Studio 帐户进行连接,以对他们进行身份验证以访问服务,类似地。也许我误解了,但我相信这是可能的DefaultAzureCredential选项,它将按顺序尝试各种身份验证方法(例如环境变量、托管标识、Visual Studio 凭据等),直到成功。
在检查与 Visual Studio 凭据流相关的内部异常时,我看到System.Exception {System.IO.DirectoryNotFoundException}
异常消息指出...
“找不到路径“C:\WINDOWS\system32\config\systemprofile\AppData\Local.IdentityService\AzureServiceAuth\tokenprovider.json”的一部分。
此前,该消息曾表述过以下消息(据我所知,这是该文件的最新位置),直到我尝试在 Visual Studio 2019 下运行进行比较,此时,它更改为上述消息。
“找不到路径 C:\Users[AppPoolName]\AppData\Local.IdentityService\AzureServiceAuth\tokenProvider.json 的一部分”。
起初,我注意到该路径不存在.IdentityService
继续,因此遵循了对此的建议MSFT 论坛帖子恢复AppAuthentification
从 VS2019 扩展到 VS2022 的配置以恢复C:\Users\<AppPoolName>\AppData\Local\.IdentityService\AzureServiceAuth\tokenprovider.json
文件和提供者TokenProviders
作为通往C:\Program Files (x86)\Microsoft Visual Studio\<version>\Enterprise\Common7\IDE\Extensions\<random dir name>\TokenService\Microsoft.Asal.TokenService.exe
。在下一个版本中,我注意到.IdentityService
已创建,但未创建正在进行的目录或文件。
然后,我多次尝试注销并进入 Visual Studio,但这似乎并没有创建剩余的丢失目录和文件。手动创建目录和文件当然可以解决System.IO.DirectoryNotFoundException
,但错误消息随后通知我文件架构不正确。我无法找到具有正确架构和值的示例。
在客户端配置选项方面,我已明确将身份验证流程模式限制为ManagedIdentity
and VisualStudioCredential
为了简单起见,在注意到其他方法(例如AzureCLI
尽管已登录,但 Azure PowerShell 模块也失败了)。
_client = new SecretClient(new Uri(options.KeyVaultUri), new DefaultAzureCredential(
new DefaultAzureCredentialOptions
{
ExcludeManagedIdentityCredential = false,
ExcludeVisualStudioCredential = false,
ExcludeInteractiveBrowserCredential = true,
ExcludeAzurePowerShellCredential = true,
ExcludeAzureCliCredential = true,
ExcludeEnvironmentCredential = true,
ExcludeVisualStudioCodeCredential = true,
ExcludeSharedTokenCacheCredential = true,
ManagedIdentityClientId = options.ManagementIdentityClientId
}
));
我也尝试过有关的建议Azure SDK GitHub 问题 #4590设置的数量setProfileEnvironment
and loadUserProfile
to true
如果这是 IIS 权限问题,但这没有什么区别 - 相同的错误仍然存在。
最后,我找到的唯一其他参考tokenProvider.json
文件位于Microsoft 的应用程序身份验证文档,但重新验证按钮不存在Tools > Options > Azure Service Authentication
按照建议的窗口。
“如果您在使用 Visual Studio 时遇到问题,例如以下错误
涉及令牌提供程序文件,请仔细检查前面的步骤。
您可能需要重新验证您的开发者令牌。为此,请选择
工具 > 选项,然后选择 Azure 服务身份验证。看
获取所选帐户下的重新验证链接。选择它以
认证。”
因为我能够找到C:\Program Files (x86)\Microsoft Visual Studio\<version>\Enterprise\Common7\IDE\Extensions\<random dir name>\TokenService\Microsoft.Asal.TokenService.exe
及其相关的配置文件,我怀疑它丢失了tokenProvider.json
文件就是问题所在,但我不知道是什么负责创建该文件,也不知道它应该包含什么。
任何见解或指示将不胜感激。
著名的软件包及其使用的版本:
- Azure.Identity @ v1.8.0
- Azure.Security.KeyVault.Secrets @ v4.4.0
Edit (1)
正如人们所期望的那样,我可以通过在 Key Vault 上授予用于 Azure AD 应用程序注册的 RBAC 记录来配置替代流程,然后使用ClientSecretCredential
流动代替DefaultAzureCredentials
流(如下)。但这并不能以最好的方式解决问题,所以如果有人能发现我的错误所在,我会很感兴趣DefaultAzureCredentials
流动,如果有的话。
_client = new SecretClient(new Uri(options.KeyVaultUri),
new ClientSecretCredential(options.TenantId, options.ClientId, options.Secret)
);