【问题标题】:Obtaining access token when User Assigned Identity is enabled启用用户分配的身份时获取访问令牌
【发布时间】:2020-06-04 23:38:10
【问题描述】:

我正在尝试使用托管标识通过 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);
}

但是,当我切换到用户分配的身份时,相同的代码会引发异常。

【问题讨论】:

    标签: azure-functions azure-managed-identity


    【解决方案1】:

    如果您想使用用户分配的身份,您需要在您的函数中指定AzureServicesAuthConnectionString 环境变量,如下所示,然后AzureServiceTokenProvider 将使用用户分配的身份来获取令牌。

    RunAs=App;AppId={ClientId of user-assigned identity} 
    

    参考 - https://docs.microsoft.com/en-us/azure/key-vault/general/service-to-service-authentication#connection-string-support

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 2015-11-18
      • 2016-04-21
      • 1970-01-01
      • 2021-10-09
      • 2014-12-09
      • 2015-05-10
      • 2017-11-22
      相关资源
      最近更新 更多