【Azure Function】Function App代码中使用Managed Identity认证获取Blob数据时遇见400报错

简介: 【Azure Function】Function App代码中使用Managed Identity认证获取Blob数据时遇见400报错

问题描述

在Azure Function中,使用如下代码读取Blob内容:

try
{
    // Retrieve the file from Azure Blob Storage
    BlobServiceClient bsclient = new BlobServiceClient(new Uri($"https://{SourceSA}.blob.core.chinacloudapi.cn"),new ManagedIdentityCredential(ClientID));
    BlobContainerClient containerClient = bsclient.GetBlobContainerClient(ContainerName);
    BlobClient sourceBlobClient = containerClient.GetBlobClient($"{sourcepathWithoutContainer}");
    MemoryStream SourceFileStream = new MemoryStream();
    await sourceBlobClient.DownloadToAsync(SourceFileStream);
    SourceFileStream.Position = 0;
    
    // ... Process Logic Process ...
}
catch (Exception ex)
{
    log.LogError(ex, "An error occurred while encrypting the file.");
    return new ObjectResult($"Error: {ex.Message}");
}

遇见了400(Bad Request)错误 ManagedIdentityCredential authentication failed: Service request failed.

在Azure Funciton所匹配的Application Insights分析页面中,发现在GET /msi/token 请求返回400。

 

问题解答

查看Function代码,发现ClientID参数取值是从Application Setting引用Azure Key Vault的Secret值来实现的。 而400的错误表示为客户端发起的请求URL有错误,所以怀疑是ClientID并不是正确的值。

进一步分析,在修改Key Vault Secret中的值后,Function App会缓存从Key Vault中获取的机密值,并且每24小时才会重新提取一次。

详见:https://learn.microsoft.com/en-us/azure/app-service/app-service-key-vault-references?tabs=azure-cli#rotation

虽然文中说明重启Function App或 App Service 可以获取到新的值,但是,有时候重启应用并没有效,还需要更多的从Application Setting中修改一次才可以缓解它。

例如:KeyVault references - are returned values cached in the App Service  : https://github.com/MicrosoftDocs/azure-docs/issues/36650

 

参考资料

  1. KeyVault references - are returned values cached in the App Service · Issue #36650 · MicrosoftDocs/azure-docs · GitHub
  2. Use Key Vault references - Azure App Service | Microsoft Learn

 

目录
打赏
0
0
0
0
194
分享
相关文章
|
21天前
|
C#
【Azure Function】Function App出现System.IO.FileNotFoundException异常
Exception while executing function: xxxxxxx,The type initializer for 'xxxxxx.Storage.Adls2.StoreDataLakeGen2Reading' threw an exception. Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the
114 64
【Azure Function】C#独立工作模式下参数类型 ServiceBusReceivedMessage 无法正常工作
Cannot convert input parameter 'message' to type 'Azure.Messaging.ServiceBus.ServiceBusReceivedMessage' from type 'System.String'.
114 73
【Azure Function】Function App门户上的Test/Run返回错误:Failed to fetch
Running your function in portal requires the app to explicitly accept requests from https://portal.azure.cn. This is known as cross-origin resource sharing (CORS).Configure CORS to add https://portal.azure.cn to allowed origins.
【Azure Function】部署Java Function失败:报错deploy [ERROR] Status code 401和警告 'China North 3' may not be a valid region
1:deploy [ERROR] Status code 401, (empty body). 2: China North 3 may not be a valid region,please refer to https://aka.ms/maven_function_configuration#supported-regions for values. 3:  <azure.functions.maven.plugin.version>1.36.0</azure.functions.maven.plugin.version>
39 11
【Azure Function】FTP上传了Python Function文件后,无法在门户页面加载函数的问题
通过FTP上传Python Function至Azure云后,出现函数列表无法加载的问题。经排查,发现是由于`requirements.txt`中的依赖包未被正确安装。解决方法为:在本地安装依赖包到`.python_packages/lib/site-packages`目录,再将该目录内容上传至云上的`wwwroot`目录,并重启应用。最终成功加载函数列表。
【Azure Function】Python Function部署到Azure后报错No module named '_cffi_backend'
ERROR: Error: No module named '_cffi_backend', Cannot find module. Please check the requirements.txt file for the missing module.
112 2
|
5月前
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub的解决之法
An exception occurred while retrieving properties for Event Hub: logicapp. Error Message: 'ClientSecretCredential authentication failed: AADSTS90002: Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud. Che
|
6月前
|
C++
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub
|
6月前
【Azure Function】Azure Function中的Timer Trigger无法自动触发问题
【Azure Function】Azure Function中的Timer Trigger无法自动触发问题

热门文章

最新文章

  • 1
    MNN-LLM App:在手机上离线运行大模型,阿里巴巴开源基于 MNN-LLM 框架开发的手机 AI 助手应用
    396
  • 2
    【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
    10
  • 3
    原生鸿蒙版小艺APP接入DeepSeek-R1,为HarmonyOS应用开发注入新活力
    39
  • 4
    【Azure App Service】基于Linux创建的App Service是否可以主动升级内置的Nginx版本呢?
    2
  • 5
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
    35
  • 6
    【05】flutter完成注册页面完善样式bug-增加自定义可复用组件widgets-严格规划文件和目录结构-规范入口文件-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
    1
  • 7
    【Azure Logic App】使用MySQL 新增行触发器遇见错误 :“Unknown column 'created_at' in 'order clause'”
    3
  • 8
    阿里云APP备案流程图以及备案所需材料整理,跟着教程一步步操作
    16
  • 9
    【07】flutter完成主页-完成底部菜单栏并且做自定义组件-完整短视频仿抖音上下滑动页面-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
    7
  • 10
    1688app 商品详情接口系列(1688API)
    6
  • AI助理

    你好,我是AI助理

    可以解答问题、推荐解决方案等