Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
APPLIES TO: All API Management tiers
In this article, you learn about ways to authenticate and authorize to Azure OpenAI API endpoints that are managed using Azure API Management. This article shows the following common methods:
Authentication - Authenticate to an Azure OpenAI API using policies that authenticate using either an API key or a Microsoft Entra ID managed identity.
Authorization - For more fine-grained access control, preauthorize requests that pass OAuth 2.0 tokens generated by an identity provider such as Microsoft Entra ID.
For background, see:
Before following the steps in this article, you must have:
A default way to authenticate to an Azure OpenAI API is by using an API key. For this type of authentication, all API requests must include a valid API key in the api-key
HTTP header.
api-key
header in requests to the Azure OpenAI API. We provide two examples of how to do this: one uses the set-backend-service
policy, and the other uses the set-header
policy.Create a backend that points to the Azure OpenAI API.
https://contoso.openai.azure.com/openai
.Add the following set-backend-service
policy snippet in the inbound
policy section to pass the API key in requests to the Azure OpenAI API.
In this example, the backend resource is openai-backend.
<set-backend-service backend-id="openai-backend" />
Alternatively, add the following set-header
policy snippet in the inbound
policy section to pass the API key in requests to the Azure OpenAI API. This policy snippet sets the api-key
header with the named value that you set up.
In this example, the named value in API Management is openai-api-key.
<set-header name="api-key" exists-action="override">
<value>{{openai-api-key}}</value>
</set-header>
An alternative and recommended way to authenticate to an Azure OpenAI API is by using a managed identity in Microsoft Entra ID. For background, see How to configure Azure OpenAI Service with managed identity.
Following are steps to configure your API Management instance to use a managed identity to authenticate requests to an Azure OpenAI API.
Enable a system-assigned or user-assigned managed identity for your API Management instance. The following example assumes that you've enabled the instance's system-assigned managed identity.
Assign the managed identity the Cognitive Services OpenAI User role, scoped to the appropriate resource. For example, assign the system-assigned managed identity the Cognitive Services OpenAI User role on the Azure OpenAI resource. For detailed steps, see Role-based access control for Azure OpenAI service.
Add the following policy snippet in the inbound
policy section to authenticate requests to the Azure OpenAI API using the managed identity.
In this example:
authentication-managed-identity
policy obtains an access token for the managed identity.set-header
policy sets the Authorization
header of the request with the access token.<authentication-managed-identity resource="https://cognitiveservices.azure.com" output-token-variable-name="managed-id-access-token" ignore-error="false" />
<set-header name="Authorization" exists-action="override">
<value>@("Bearer " + (string)context.Variables["managed-id-access-token"])</value>
</set-header>
To enable more fine-grained access to OpenAPI APIs by particular users or clients, you can preauthorize access to the Azure OpenAI API using OAuth 2.0 authorization with Microsoft Entra ID or another identity provider. For background, see Protect an API in Azure API Management using OAuth 2.0 authorization with Microsoft Entra ID.
Note
Use OAuth 2.0 authorization as part of a defense-in-depth strategy. It's not a replacement for API key authentication or managed identity authentication to an Azure OpenAI API.
Following are high level steps to restrict API access to users or apps that are authorized using an identity provider.
Create an application in your identity provider to represent the OpenAI API in Azure API Management. If you're using Microsoft Entra ID, register an application in your Microsoft Entra ID tenant. Record details such as the application ID and the audience URI.
As needed, configure the application to have roles or scopes that represent the fine-grained permissions needed to access the Azure OpenAI API.
Add an inbound
policy snippet in your API Management instance to validate requests that present a JSON web token (JWT) in the Authorization
header. Place this snippet before other inbound
policies that you set to authenticate to the Azure OpenAI API.
Note
The following examples show the general structure of the policies to validate a JWT. Customize them to your identity provider and the requirements of your application and API.
validate-azure-ad-token - If you use Microsoft Entra ID, configure the validate-azure-ad-token
policy to validate the audience and claims in the JWT. For details, see the policy reference.
<validate-azure-ad-token tenant-id={{TENANT_ID}} header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<client-application-ids>
<application-id>{{CLIENT_APP_ID}}</application-id>
</client-application-ids>
<audiences>
<audience>...</audience>
</audiences>
<required-claims>
<claim name=...>
<value>...</value>
</claim>
</required-claims>
</validate-azure-ad-token>
validate-jwt - If you use another identity provider, configure the validate-jwt
policy to validate the audience and claims in the JWT. For details, see the policy reference.
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url={{OPENID_CONFIGURATION_URL}} />
<issuers>
<issuer>{{ISSUER_URL}}</issuer>
</issuers>
<audiences>
<audience>...</audience>
</audiences>
<required-claims>
<claim name=...>
<value>...</value>
</claim>
</required-claims>
</validate-jwt>
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Secure Azure OpenAI authentication and authorization - Training
Learn about the security considerations for different ways of authenticating to Azure OpenAI and how to assign role based access control permissions to managed identities
Certification
Microsoft Certified: Azure AI Engineer Associate - Certifications
Design and implement an Azure AI solution using Azure AI services, Azure AI Search, and Azure Open AI.
Documentation
Import an Azure OpenAI API as REST API - Azure API Management
How to import an Azure OpenAI API as a REST API from the Azure OpenAI Service or from an OpenAPI specification.
How to configure Azure OpenAI Service with Microsoft Entra ID authentication - Azure OpenAI
Provides guidance on how to set managed identity with Microsoft Entra ID
Azure OpenAI Service REST API reference - Azure OpenAI
Learn how to use Azure OpenAI's REST API. In this article, you learn about authorization options, how to structure a request and receive a response.