标签: azure-managed-identity

如何获取为特定用户分配的 Azure 应用服务托管服务标识的令牌?

我正在尝试获取特定用户定义身份的 msi 令牌。我们的应用服务有 2 个用户定义的身份,我想要一个代表用户分配的身份之一的令牌。

这是代码:

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(
            "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/&object_id=<ObjectId>&client_id=<clientId>");

        req.Headers["Metadata"] = "true";
        req.Method = "GET";

        try
        {
            // Call /token endpoint
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();

            // Pipe response Stream to a StreamReader, and extract access token
            StreamReader streamResponse = new StreamReader(response.GetResponseStream());
            string stringResponse = streamResponse.ReadToEnd();
            Dictionary<string, string> list =
                 JsonConvert.DeserializeObject<Dictionary<string, string>>(stringResponse);
            string accessToken = list["access_token"];

            System.IO.File.WriteAllText(@".\Log.txt", accessToken);
        }
        catch (Exception e)
        {
            string errorText = String.Format("{0} \n\n{1}", e.Message, e.InnerException != null ? e.InnerException.Message : "Acquire token failed");
            System.IO.File.WriteAllText(@".\Log.txt", …
Run Code Online (Sandbox Code Playgroud)

azure azure-active-directory azure-web-app-service azure-managed-identity

1
推荐指数
1
解决办法
2458
查看次数

启用用户分配的身份时获取访问令牌

我正在尝试使用托管标识通过 azure 门户访问 Azure Function。如果我使用系统分配的身份,下面的代码行效果很好。

  [FunctionName("FunctionDemo")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req,
ILogger log)
{
    log.LogInformation("Starting to get accessToken through client id");
    string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/")
    return req.CreateResponse(HttpStatusCode.OK);
}
Run Code Online (Sandbox Code Playgroud)

但是,当我切换到用户分配的身份时,相同的代码会引发异常。 在此处输入图片说明

azure-functions azure-managed-identity

1
推荐指数
1
解决办法
174
查看次数

将自定义角色分配给 Azure Active Directory 系统托管标识

为了在 API 之间建立 Azure AD 系统托管标识,我在应用程序清单中为目标 API 定义了一个自定义角色。

"appRoles": [
    {
        "allowedMemberTypes": [
            "Application"
        ],
        "description": "Allow the application to read all things as itself.",
        "displayName": "Read all things",
        "id": "86a914fa-a862-4962-9975-be5c9a05dca3",
        "isEnabled": true,
        "lang": null,
        "origin": "Application",
        "value": "Things.Read.All"
    }
Run Code Online (Sandbox Code Playgroud)

现在,我想将此角色分配给要调用它的 api,以便我可以在从 AzureServiceTokenProvider 收到的访问令牌中验证它。问题是我在应用程序注册中没有看到系统分配的身份。

身份(声明系统分配身份的位置)“Azure 角色分配”下有一个按钮,可用于添加角色分配。此处有可用的角色列表。我正在寻找我定义的自定义角色,它不在下拉列表中。

如何将定义的角色分配给系统身份,以便它可以访问允许的 api 或其他 api?我希望在访问令牌中获得此角色。这是正确的期望吗?

azure-active-directory azure-managed-identity

1
推荐指数
1
解决办法
1894
查看次数

使用 AKS 中的用户管理标识、Python SDK 时出现 AzureBlobStorage AuthorizationPermissionMismatch 错误

我正在 AKS 中运行 Python 应用程序(作为作业,但并不重要),使用 Azure Python SDK 访问 blob 存储。我使用用户管理身份进行身份验证,ManagedIdentityCredentialclient_idkwarg 一起使用(请参阅https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.drivenidentitycredential?view=azure-python)。它能够成功查询 IMDS 端点并获取令牌,但我仍然遇到错误。有人知道我可能缺少什么设置吗?

关于用户管理身份的整体文档很少,特别是与 AKS 和 blob 存储相关的文档,以及此错误。

成功获取 IMDS 令牌:

2021-01-26 05:26:05,944 - azure.core.pipeline.policies.http_logging_policy - INFO - Request URL: 'http://REDACTED/metadata/identity/oauth2/token?api-version=REDACTED&resource=REDACTED&client_id=REDACTED'
2021-01-26 05:26:05,945 - azure.core.pipeline.policies.http_logging_policy - INFO - Request method: 'GET'
2021-01-26 05:26:05,945 - azure.core.pipeline.policies.http_logging_policy - INFO - Request headers:
2021-01-26 05:26:05,945 - azure.core.pipeline.policies.http_logging_policy - INFO -     'Metadata': 'REDACTED'
2021-01-26 05:26:05,945 - azure.core.pipeline.policies.http_logging_policy - INFO -     'User-Agent': 'azsdk-python-identity/1.5.0 Python/3.7.7 (Linux-4.15.0-1103-azure-x86_64-with-debian-9.12)'
2021-01-26 05:26:05,945 - …
Run Code Online (Sandbox Code Playgroud)

azure-active-directory azure-cli azure-blob-storage azure-managed-identity azure-aks

1
推荐指数
1
解决办法
8635
查看次数

尝试将托管标识与 Azure 服务总线一起使用

我尝试按照教程对我的服务总线进行身份验证DefaultAzureCredentials,但是,我收到了 401。

我在设置中使用以下代码:

services.AddAzureClients(x =>
{
    x.AddServiceBusClientWithNamespace("myns.servicebus.windows.net")
        .WithCredential(new Azure.Identity.DefaultAzureCredential());
});
Run Code Online (Sandbox Code Playgroud)

然后我像这样调用 SB 客户端:

var sender = client.CreateSender("myqueue");
var message = new ServiceBusMessage(Encoding.UTF8.GetBytes("test"));

await sender.SendMessageAsync(message);
Run Code Online (Sandbox Code Playgroud)

当我打电话时,SendMessageAsync我收到 401 错误:

失败:Azure-Messaging-ServiceBus[82] 为标识符创建发送链接时发生异常:myqueue-578624f3-f732-4a9b-2ab0-9adc01949a5a。错误消息:'System.UnauthorizedAccessException:放置令牌失败。状态代码:401,状态描述:InvalidIssuer:令牌发行者无效。TrackingId:cde3a89c-8108-48d1-8b8f-dacde18e176f, SystemTracker:NoSystemTracker, Timestamp:2021-05-19T07:18:44。

在我运行它之前,我调用az login. 我可以访问命名空间来发送和接收。我的猜测是我需要在服务总线和......之间分配某种权限 - 但由于我将它作为控制台应用程序运行,我使用自己的凭据运行。显然,我不明白有关托管身份的某些内容。

编辑:

根据@juunas 的建议,我尝试了以下操作:

services.AddHostedService<ConsoleHostedService>();
services.AddAzureClients(x =>
{
    //var creds = new Azure.Identity.EnvironmentCredential(); // 1st - EnvironmentCredential authentication unavailable. Environment variables are not fully configured.'
    //var creds = new Azure.Identity.ManagedIdentityCredential(); // 2nd - No Managed Identity endpoint …
Run Code Online (Sandbox Code Playgroud)

azure azureservicebus azure-managed-identity

1
推荐指数
1
解决办法
198
查看次数

值不能为空。(参数“sharedKeyCredential”)当我们尝试使用 V12 的GenerateSasUri() 方法创建 SAS Uri 时

我们正在将代码从 V11 迁移到使用 azure storage v12 客户端库 (Azure.Storage.Blobs 12.12.0)。当我们尝试使用GenerateSasUri()方法创建SAS Uri时,出现下面提到的异常。
异常:“值不能为空。(参数‘sharedKeyCredential’)”

this.blobContainerClient = new BlobContainerClient(
                new Uri($https://{storageAccountName}.blob.core.windows.net/{containerName}),
                new ManagedIdentityCredential(managedIdentityAppId));

var blobClient = blobContainerClient.GetBlobClient(blobName);

            BlobSasBuilder sasBuilder = new()
            {
                BlobContainerName = containerName,
                BlobName = blobName,
                Resource = "b", 
                StartsOn = DateTime.UtcNow.AddMinutes(-15),
                ExpiresOn = expirationTimeUtc
            };
            sasBuilder.SetPermissions(requestedPermission);            

            return blobClient.GenerateSasUri(sasBuilder);
Run Code Online (Sandbox Code Playgroud)

c# storage azure azure-managed-identity

0
推荐指数
1
解决办法
2377
查看次数