title | description | services | documentationcenter | author | manager | editor | ms.service | ms.workload | ms.tgt_pltfrm | ms.devlang | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Protect an API by using OAuth 2.0 with Azure Active Directory and API Management | Microsoft Docs |
Learn how to protect a web API backend with Azure Active Directory and API Management. |
api-management |
miaojiang |
cfowler |
api-management |
mobile |
na |
na |
article |
03/18/2018 |
apimpm |
This guide shows you how to configure your Azure API Management instance to protect an API, by using the OAuth 2.0 protocol with Azure Active Directory (Azure AD).
To follow the steps in this article, you must have:
- An API Management instance
- An API being published that uses the API Management instance
- An Azure AD tenant
Here is a quick overview of the steps:
- Register an application (backend-app) in Azure AD to represent the API.
- Register another application (client-app) in Azure AD to represent a client application that needs to call the API.
- In Azure AD, grant permissions to allow the client-app to call the backend-app.
- Configure the Developer Console to use OAuth 2.0 user authorization.
- Add the validate-jwt policy to validate the OAuth token for every incoming request.
To protect an API with Azure AD, the first step is to register an application in Azure AD that represents the API.
-
Browse to your Azure AD tenant, and then browse to App registrations.
-
Select New application registration.
-
Provide a name of the application. (For this example, the name is
backend-app
.) -
Choose Web app / API as the Application type.
-
For Sign-on URL, you can use
https://localhost
as a placeholder. -
Select Create.
When the application is created, make a note of the Application ID, for use in a subsequent step.
Every client application that calls the API needs to be registered as an application in Azure AD as well. For this example, the sample client application is the Developer Console in the API Management developer portal. Here's how to register another application in Azure AD to represent the Developer Console.
-
Select New application registration.
-
Provide a name of the application. (For this example, the name is
client-app
.) -
Choose Web app / API as the Application type.
-
For Sign-on URL, you can use
https://localhost
as a placeholder, or use the sign-in URL of your API Management instance. (For this example, the URL ishttps://contoso5.portal.azure-api.net/signin
.) -
Select Create.
When the application is created, make a note of the Application ID, for use in a subsequent step.
Now, create a client secret for this application, for use in a subsequent step.
-
Select Settings again, and go to Keys.
-
Under Passwords, provide a Key description. Choose when the key should expire, and select Save.
Make a note of the key value.
Now that you have registered two applications to represent the API and the Developer Console, you need to grant permissions to allow the client-app to call the backend-app.
-
Browse to Application registrations.
-
Select
client-app
, and go to Settings. -
Select Required permissions > Add.
-
Select Select an API, and search for
backend-app
. -
Under Delegated Permissions, select
Access backend-app
. -
Select Select, and then select Done.
Note
If Azure Active Directory is not listed under permissions to other applications, select Add to add it from the list.
At this point, you have created your applications in Azure AD, and have granted proper permissions to allow the client-app to call the backend-app.
In this example, the Developer Console is the client-app. The following steps describe how to enable OAuth 2.0 user authorization in the Developer Console.
-
Browse to your API Management instance.
-
Select OAuth 2.0 > Add.
-
Provide a Display name and Description.
-
For the Client registration page URL, enter a placeholder value, such as
http://localhost
. The Client registration page URL points to the page that users can use to create and configure their own accounts for OAuth 2.0 providers that support this. In this example, users do not create and configure their own accounts, so you use a placeholder instead. -
For Authorization grant types, select Authorization code.
-
Specify the Authorization endpoint URL and Token endpoint URL. Retrieve these values from the Endpoints page in your Azure AD tenant. Browse to the App registrations page again, and select Endpoints.
-
Copy the OAuth 2.0 Authorization Endpoint, and paste it into the Authorization endpoint URL text box.
-
Copy the OAuth 2.0 Token Endpoint, and paste it into the Token endpoint URL text box. In addition to pasting in the token endpoint, add a body parameter named resource. For the value of this parameter, use the Application ID for the back-end app.
-
Next, specify the client credentials. These are the credentials for the client-app.
-
For Client ID, use the Application ID for the client-app.
-
For Client secret, use the key you created for the client-app earlier.
-
Immediately following the client secret is the redirect_url for the authorization code grant type. Make a note of this URL.
-
Select Create.
-
Go back to the Settings page of your client-app.
-
Select Reply URLs, and paste the redirect_url in the first row. In this example, you replaced
https://localhost
with the URL in the first row.
Now that you have configured an OAuth 2.0 authorization server, the Developer Console can obtain access tokens from Azure AD.
The next step is to enable OAuth 2.0 user authorization for your API. This enables the Developer Console to know that it needs to obtain an access token on behalf of the user, before making calls to your API.
-
Browse to your API Management instance, and go to APIs.
-
Select the API you want to protect. In this example, you use the
Echo API
. -
Go to Settings.
-
Under Security, choose OAuth 2.0, and select the OAuth 2.0 server you configured earlier.
-
Select Save.
Now that the OAuth 2.0 user authorization is enabled on the Echo API
, the Developer Console obtains an access token on behalf of the user, before calling the API.
-
Browse to any operation under the
Echo API
in the developer portal, and select Try it. This brings you to the Developer Console. -
Note a new item in the Authorization section, corresponding to the authorization server you just added.
-
Select Authorization code from the authorization drop-down list, and you are prompted to sign in to the Azure AD tenant. If you are already signed in with the account, you might not be prompted.
-
After successful sign-in, an
Authorization
header is added to the request, with an access token from Azure AD. The following is a sample token (Base64 encoded):Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlNTUWRoSTFjS3ZoUUVEU0p4RTJnR1lzNDBRMCIsImtpZCI6IlNTUWRoSTFjS3ZoUUVEU0p4RTJnR1lzNDBRMCJ9.eyJhdWQiOiIxYzg2ZWVmNC1jMjZkLTRiNGUtODEzNy0wYjBiZTEyM2NhMGMiLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC80NDc4ODkyMC05Yjk3LTRmOGItODIwYS0yMTFiMTMzZDk1MzgvIiwiaWF0IjoxNTIxMTUyNjMzLCJuYmYiOjE1MjExNTI2MzMsImV4cCI6MTUyMTE1NjUzMywiYWNyIjoiMSIsImFpbyI6IkFWUUFxLzhHQUFBQUptVzkzTFd6dVArcGF4ZzJPeGE1cGp2V1NXV1ZSVnd1ZXZ5QU5yMlNkc0tkQmFWNnNjcHZsbUpmT1dDOThscUJJMDhXdlB6cDdlenpJdzJLai9MdWdXWWdydHhkM1lmaDlYSGpXeFVaWk9JPSIsImFtciI6WyJyc2EiXSwiYXBwaWQiOiJhYTY5ODM1OC0yMWEzLTRhYTQtYjI3OC1mMzI2NTMzMDUzZTkiLCJhcHBpZGFjciI6IjEiLCJlbWFpbCI6Im1pamlhbmdAbWljcm9zb2Z0LmNvbSIsImZhbWlseV9uYW1lIjoiSmlhbmciLCJnaXZlbl9uYW1lIjoiTWlhbyIsImlkcCI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzcyZjk4OGJmLTg2ZjEtNDFhZi05MWFiLTJkN2NkMDExZGI0Ny8iLCJpcGFkZHIiOiIxMzEuMTA3LjE3NC4xNDAiLCJuYW1lIjoiTWlhbyBKaWFuZyIsIm9pZCI6IjhiMTU4ZDEwLWVmZGItNDUxMS1iOTQzLTczOWZkYjMxNzAyZSIsInNjcCI6InVzZXJfaW1wZXJzb25hdGlvbiIsInN1YiI6IkFGaWtvWFk1TEV1LTNkbk1pa3Z3MUJzQUx4SGIybV9IaVJjaHVfSEM1aGciLCJ0aWQiOiI0NDc4ODkyMC05Yjk3LTRmOGItODIwYS0yMTFiMTMzZDk1MzgiLCJ1bmlxdWVfbmFtZSI6Im1pamlhbmdAbWljcm9zb2Z0LmNvbSIsInV0aSI6ImFQaTJxOVZ6ODBXdHNsYjRBMzBCQUEiLCJ2ZXIiOiIxLjAifQ.agGfaegYRnGj6DM_-N_eYulnQdXHhrsus45QDuApirETDR2P2aMRxRioOCR2YVwn8pmpQ1LoAhddcYMWisrw_qhaQr0AYsDPWRtJ6x0hDk5teUgbix3gazb7F-TVcC1gXpc9y7j77Ujxcq9z0r5lF65Y9bpNSefn9Te6GZYG7BgKEixqC4W6LqjtcjuOuW-ouy6LSSox71Fj4Ni3zkGfxX1T_jiOvQTd6BBltSrShDm0bTMefoyX8oqfMEA2ziKjwvBFrOjO0uK4rJLgLYH4qvkR0bdF9etdstqKMo5gecarWHNzWi_tghQu9aE3Z3EZdYNI_ZGM-Bbe3pkCfvEOyA
-
Select Send, and you can call the API successfully.
At this point, when a user tries to make a call from the Developer Console, the user is prompted to sign in. The Developer Console obtains an access token on behalf of the user.
But what if someone calls your API without a token or with an invalid token? For example, you can still call the API even if you delete the Authorization
header. The reason is that API Management does not validate the access token at this point. It simply passes the Authorization
header to the back-end API.
You can use the Validate JWT policy to pre-authorize requests in API Management, by validating the access tokens of each incoming request. If a request does not have a valid token, API Management blocks it. For example, you can add the following policy to the <inbound>
policy section of the Echo API
. It checks the audience claim in an access token, and returns an error message if the token is not valid. For information on how to configure policies, see Set or edit policies.
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://login.microsoftonline.com/{aad-tenant}/.well-known/openid-configuration" />
<required-claims>
<claim name="aud">
<value>{Application ID of backend-app}</value>
</claim>
</required-claims>
</validate-jwt>
In this guide, you used the Developer Console in API Management as the sample client application to call the Echo API
protected by OAuth 2.0. To learn more about how to build an application and implement OAuth 2.0, see Azure Active Directory code samples.
-
Learn more about Azure Active Directory and OAuth2.0.
-
Check out more videos about API Management.
-
For other ways to secure your back-end service, see Mutual Certificate authentication.