简体   繁体   English

如何使用 C# 中的托管标识对 Azure 资源管理器进行身份验证

[英]How to authenticate with Azure Resource Manager using managed identity in C#

I am trying to use the managedIdentity to get a token that I can then use to list resources in the resource group.我正在尝试使用 managedIdentity 获取一个令牌,然后我可以使用该令牌列出资源组中的资源。 I am getting an error when attempting to get the token.尝试获取令牌时出现错误。 The GetToken() api seems to be adding other strings (offline_access openid) internally to the scope I provided and fails that the scope is not a valid url. From the error, it appears that I am not using the api correctly. GetToken() api 似乎在内部向我提供的 scope 添加其他字符串(offline_access openid),但失败了,因为 scope 不是有效的 url。从错误来看,我似乎没有正确使用 api。 But I am also following the documentation.但我也在关注文档。 Could some one please help track down what the issue is in my code.有人可以帮助找出我的代码中的问题所在吗?

Code:代码:

var managedIdentityCredential = new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = managedIdentityId });
var azureServiceTokenProvider = new AzureServiceTokenProvider("RunAs=App");
AccessToken accessToken = managedIdentityCredential.GetToken(new TokenRequestContext(new string[] { "https://management.azure.com/" }));

Error: Azure.Identity.AuthenticationFailedException: SharedTokenCacheCredential authentication failed.错误:Azure.Identity.AuthenticationFailedException:SharedTokenCacheCredential 身份验证失败。 ---> Microsoft.Identity.Client.MsalServiceException: AADSTS70011: The provided request must include a 'scope' input parameter. ---> Microsoft.Identity.Client.MsalServiceException:AADSTS70011:提供的请求必须包含“范围”输入参数。 The provided value for the input parameter 'scope' is not valid.为输入参数“scope”提供的值无效。 The scope https://management.azure.com/ offline_access openid profile is not valid. scope https://management.azure.com/offline_access openid 配置文件无效。 The scope format is invalid. scope 格式无效。 Scope must be in a valid URI form https://example/scope or a valid Guid <guid/scope>. Scope 必须采用有效的 URI 形式https://example/scope或有效的 Guid <guid/scope>。

To get the token with MSI(managed identity), make sure you ran the code in the Azure services that support the managed identity .要使用 MSI(托管身份)获取令牌,请确保您在支持托管身份的 Azure 服务中运行了代码。

After enabling the system-assigned MSI for the service, then use the code below directly.为服务启用系统分配的MSI后,直接使用下面的代码。

var azureServiceTokenProvider = new AzureServiceTokenProvider("RunAs=App");
string accessToken = azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/").Result;
Console.WriteLine(accessToken)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure 存储帐户使用托管标识和 C# 进行身份验证 - Azure Storage Account authenticate using Managed Identity and C# 无法使用 C# Azure Function 中的系统托管标识从 Key Vault 获取机密值 - Unable to fetch secret value from Key Vault using system managed identity in C# Azure Function 访问 C# 中 Azure 中的系统托管标识的应用程序 ID - Access application id of a system managed identity in Azure in C# 如何使用 c# 针对 Azure AD 验证用户密码 - How to Authenticate Pasword of User against Azure AD using c# C#使用Azure资源管理器(ARM)删除Azure虚拟机时获取结果状态 - C# Getting result status when deploing Azure Virtual Machine using Azure Resource Manager(ARM) 对部署到 Azure 的应用程序使用 Azure 托管标识? - Using Azure Managed Identity for app deployed to Azure? 如何使用托管标识删除 Azure 批处理池和作业? - How to delete Azure Batch Pool and Jobs using Managed Identity? 通过托管标识从 c# Azure 函数检索配置值的 Azure 应用程序配置不起作用 - Azure App Configuration to retrieve configuration values from c# Azure function via Managed Identity is not working 从 Azure Web 应用托管服务标识创建资源组 - Creating resource groups from Azure Web App Managed Service Identity 如何在C#中的Dispose()方法中配置托管资源? - How to dispose managed resource in Dispose() method in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM