【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

 

相关文章
|
4天前
|
监控 关系型数据库 MySQL
|
1天前
|
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
86 61
|
17天前
|
JSON C# 数据格式
【Azure Function】C#独立工作模式下参数类型 ServiceBusReceivedMessage 无法正常工作
Cannot convert input parameter 'message' to type 'Azure.Messaging.ServiceBus.ServiceBusReceivedMessage' from type 'System.String'.
105 73
|
19天前
|
Windows
【Azure App Service】对App Service中CPU指标数据中系统占用部分(System CPU)的解释
在Azure App Service中,CPU占比可在App Service Plan级别查看整个实例的资源使用情况。具体应用中仅能查看CPU时间,需通过公式【CPU Time / (CPU核数 * 60)】估算占比。CPU百分比适用于可横向扩展的计划(Basic、Standard、Premium),而CPU时间适用于Free或Shared计划。然而,CPU Percentage包含所有应用及系统占用的CPU,高CPU指标可能由系统而非应用请求引起。详细分析每个进程的CPU占用需抓取Windows Performance Trace数据。
78 40
|
23天前
|
Java Windows
【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>
33 11
|
2月前
|
API
【Azure Logic App】使用Logic App来定制Monitor Alert邮件内容遇见无法获取SearchResults的情况
Log search alert rules from API version 2020-05-01 use this payload type, which only supports common schema. Search results aren't embedded in the log search alerts payload when you use this version.
49 10
|
3月前
|
中间件 Docker Python
【Azure Function】FTP上传了Python Function文件后,无法在门户页面加载函数的问题
通过FTP上传Python Function至Azure云后,出现函数列表无法加载的问题。经排查,发现是由于`requirements.txt`中的依赖包未被正确安装。解决方法为:在本地安装依赖包到`.python_packages/lib/site-packages`目录,再将该目录内容上传至云上的`wwwroot`目录,并重启应用。最终成功加载函数列表。
|
4月前
|
JavaScript
箭头函数与普通函数(function)的区别
箭头函数是ES6引入的新特性,与传统函数相比,它有更简洁的语法,且没有自己的this、arguments、super或new.target绑定,而是继承自外层作用域。箭头函数不适用于构造函数,不能使用new关键字调用。
|
4月前
|
数据可视化 开发者 索引
详解Wireshark LUA插件函数:function p_myproto.dissector(buffer, pinfo, tree)
在 Wireshark 中,LUA 插件通过 `function p_myproto.dissector(buffer, pinfo, tree)` 扩展协议解析能力,解析自定义应用层协议。参数 `buffer` 是 `PacketBuffer` 类型,表示原始数据包内容;`pinfo` 是 `ProtoInfo` 类型,包含数据包元信息(如 IP 地址、协议类型等);`tree` 是
147 1

热门文章

最新文章

  • 1
    DeepSeek Artifacts:在线实时预览的前端 AI 编程工具,基于DeepSeek V3快速生成React App
  • 2
    圈子社交app前端+后端源码,uniapp社交兴趣圈子开发,框架php圈子小程序安装搭建
  • 3
    【05】2025年1月首发完整版-篇幅较长-苹果app如何上架到app store完整流程·不借助第三方上架工具的情况下无需花钱但需仔细学习-优雅草央千澈详解关于APP签名以及分发-们最关心的一篇来了-IOS上架app
  • 4
    【05】flutter完成注册页面完善样式bug-增加自定义可复用组件widgets-严格规划文件和目录结构-规范入口文件-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
  • 5
    电竞陪玩系统架构优化设计,陪玩app如何提升系统稳定性,陪玩小程序平台的测试与监控
  • 6
    京东商品详情数据接口(H5、APP 端)
  • 7
    年轻人如何运用圈子系统进行扩列,社交圈子论坛app扩列的好处,兴趣行业圈子提升社交技能
  • 8
    【Azure App Service】对App Service中CPU指标数据中系统占用部分(System CPU)的解释
  • 9
    【Azure Logic App】使用MySQL 新增行触发器遇见错误 :“Unknown column 'created_at' in 'order clause'”
  • 10
    app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈