我需要获取服务主体的不记名访问 token 。我想在 C# 应用程序中使用它。
鉴于我有主体 ID、 secret 和租户 ID,我如何获得它?
编辑:
更具体:
我有 client_id
和 client_secret
的服务主体。
我可以使用以下命令通过 azure cli 获取不记名 token
az login --service-principal -u client_id --tenant my_tenant_domain -p client_secret
az account set --subscription my_subscription_id
az account get-access-token
我想在不使用 CLI 的情况下获得相同的 token ,即使用 Azure SDK 进行 dot net 或 rest 调用
最佳答案
我最终得到了以下代码:
var adSettings = new ActiveDirectoryServiceSettings
{
AuthenticationEndpoint = new Uri(AzureEnvironment.AzureGlobalCloud.AuthenticationEndpoint),
TokenAudience = new Uri(AzureEnvironment.AzureGlobalCloud.ManagementEndpoint),
ValidateAuthority = true
};
await ApplicationTokenProvider.LoginSilentAsync(
TenantId.Value,
ServicePrincipalId,
ServicePrincipalSecret,
adSettings,
TokenCache.DefaultShared);
var token = TokenCache.DefaultShared.ReadItems()
.Where(t => t.ClientId == ServicePrincipalId)
.OrderByDescending(t => t.ExpiresOn)
.First();
关于.net - 如何使用 C# 获取 azure 服务主体的不记名 token ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54113938/