分享好友 最新动态首页 最新动态分类 切换频道
Authenticate your Azure OpenAI Based App - Key or Key-less
2024-12-26 13:52

If you’re using Azure OpenAI, then you must be aware that the most common and easiest way to authenticate our application is using app-key.

The key-based authentication approach is very popular because it is very straightforward. Let's have a quick look at the below code snippet:



The above code snippet constructs the AzureOpenAI client object using api-key,api-version, and endpoint. Then, this object makes a call to the completion endpoint with the required parameters.

Of course, key-based authentication works very well for experimentation purposes, like in the above case, but it is not very well suited for enterprise-grade applications that will soon be in production.

So, why should we never use keys to authenticate Azure OpenAI in production?

In a production capacity, it is generally not recommended to use keys for Azure OpenAI authentication, and there are several key reasons behind for this. Here are the reasons.

  • Key Exposure: Keys may inadvertently be committed to source control by developers or stored in some insecure locations, and if the key is exposed, unauthorized individuals can easily gain access to the Azure environment.
  • Granular Permissions: Keys are good for local development and testing but not for production environments as they don’t follow the principle of least privilege. They give too much access to anyone who uses them, which can lead to misuse.
  • Shareable: It is very easy to share keys with both good and bad intentions, intentionally or unintentionally which leads to potential security breaks.
  • Rotation being hard: As keys can’t rotate automatically, we need manual intervention to do the rotation which might be too time-consuming and cumbersome, specifically in a production deployment. Just one instance of compromise and all the instances where that are used to generate tokens will need an update.

One should avoid using keys to authenticate and use Managed Identities for Azure resources. There are multiple benefits of Managed Identities.

  • Superior security: Managed identities offer a significant improvement to security by providing tokens which are generated by Azure. This eventually means, that we no longer need to handle credentials directly.
  • Granular Access Control: With Managed Identities, assigning specific roles and permissions is straightforward. This ensures that each identity has only the access it needs, adhering to the principle of least privilege. It emphasizes on granting the bare minimum required permissions.
  • No rotation required: As the managed identity feature takes care of credential management for us, we do not need to store or manually rotate credentials, which minimizes the risk of credential exposure.

Let’s get started by updating our source code first. Here are the code changes.



Now, if you simply execute this code, it won’t work. Here is the error, you can expect.

The error clearly says, there is something wrong with the authentication part and this happened because of these two reasons.

  • no default credentials are set
  • no permissions are granted to the user, who is executing the application

Here, we have multiple ways to sort out our default credential issue. Here is the complete documentation of the DefaultAzureCredential class, which sits in Azure. Identity namespace.

You can choose any of these ways to resolve this. I used CLI and executed the commands below in the PowerShell terminal.



If you’re not able to execute the above commands then make sure you’ve required PowerShell modules installed:



Once you get connected, you will see that your available subscriptions are listed in your terminal.

The next step is to make sure that whoever is running the code has permission to access the OpenAI service. This can be done either with Azure CLI or using the Azure portal.

Using Azure CLI (make sure to set required environment variables).


最新文章
2025年全国研究生招生考试暨南大学(4418)考点考生考试须知
2025年全国硕士研究生招生考试(初试)将于12月21-22日进行,暨南大学考点2025年全国研究生招生考试暨南大学考点考生考试须知如下。一、提前规划进校路线。暨南大学考点设体育馆、教学大楼、附中楼三个考区。考生请在考前自行登录“中国研
AI+更注重智能化应用场景
  中央经济工作会议12月11日至12日在北京举行。会议再提开展“人工智能+”行动,培育未来产业。   据南都记者了解,“人工智能+”并非新提法。今年3月,国务院政府工作报告首次提出开展“人工智能+”行动;此次中央经济工作会议再次点
CryptoAutos(AUTOS)币是什么?前景如何?值得投资吗?
CryptoAutos(AUTOS)币将于2024.12.11在Gate.io交易所上线交易,CryptoAutos必定将被大规模使用。如何实现?我们正在为所有加密货币添加现实世界的实用性,并通过即时支付商品和服务,缩小 Web2 和 Web3 之间的差距。那么,CryptoAutos(AUTO
2024年华南理工大学新生开学报到时间及入学指南注意事项
寒窗十二载,今朝圆梦。距离华南理工大学开学还有很短的时间了,你开始为大学入学做准备了吗?51升学网小编特为大家送来了关于华南理工大学新生入学及开学注意事项的相关信息的文章,愿你到时不会手忙脚乱,建议分享给身边的家长和同学!本
1997央视春晚丨赵本山范伟小品《红高粱模特队》剧本台词
队员众:(跑上台)队长,教练到。赵队长:归队,敬礼,队伍滑过来,向我这看,欢迎欢迎!范教练:大家好……你好你好……赵队长:自我介绍一下,免贵姓赵,这次进京我任队长。范教练:哦,赵队长……赵队长:兼裁缝。范教练:哈……自我介
2024双十一首波“最受欢迎”的10款手机,苹果仅排第三,荣耀第二
​​谁说手机市场已经跌入寒冬?最新出炉的双11手机热销榜单给市场带来了一股暖流。作为一个混迹科技圈多年的老编辑,每次看榜单都像在读一本故事书,里面写满了品牌的野心、产品的温度,还有消费者的精打细算。首先,划重点!买手机前先领
AI热潮席卷各行业,重点企业引领前沿
当前,AI已然成为驱动产业变革的强劲引擎,深度嵌入诸多领域,催生出一片繁荣景象,不少企业紧抓机遇、乘势而上。以下是各细分赛道中值得关注的重点企业。在AI电商赛道,焦点科技与小商品城脱颖而出。焦点科技依托先进AI技术,优化跨境与内
2025年全国硕士研究生招生考试(初试)武汉工程大学考点(4228)考生须知
各位考生:2025年全国硕士研究生招生考试(以下简称研考)将于2024年12月21至22日举行。为确保考试平稳顺利,现就有关事项温馨提示如下: 一、备好证件,妥善保管请考生自行登录“中国研究生招生信息网”(http://yz.chsi.com.cn/)下载、
H5 推广页直接跳转小程序,究竟是怎么做到的?
在当今的移动互联网营销领域,H5 推广页与小程序的结合愈发重要。H5 网页直接跳转小程序有着诸多显著优势,它能够极大地缩短推广引流的流程,让流量转换效率得到显著提升。不过,需要注意的是,H5 网页本身并不具备直接跳转其他应用程序的
docker部署一个非常强大的工具箱:it-tools(五星推荐)
it-tools是一个非常实用的IT工具箱,包含了:二维码生成、各类数据格式转换、mac地址生成等功能,尤其是"docker run转docker-compose"非常好用。不仅如此it-tools可以通过docker部署,使用非常方便。唯一缺点是it-tools暂时没有中文选项,
相关文章
推荐文章
发表评论
0评