In this section, you'll:
- Create an API management instance
- Import the Basic Calculator API
- Configure an OAuth 2.0 Server
- Set Auth0 as the OAuth 2.0 Server handling authentication requests to the API
- Test the Auth0-Azure API integration
Step 1: Create Your API Management Instance
To create a new API management service, click on New > Web + Mobile > API management.
You'll be asked to provide the following configuration variables:
|Name||The name for your service (which will also be used to create the URL you need to access the service)|
|Subscription||The Azure subscription plan with which you'll use with the service|
|Resource group||The collection of resources sharing a lifecycle, permissions, and policies. You can use an existing resource group or you can create a new one (you'll need to provide a name for the group if you create a new one)|
|Location||Choose the location that services your API instance|
|Organization name||The name of your organization|
|Administrator email||The email address of the person who will be administering this instance|
|Pricing tier||The pricing tier you want, which determines the number of calls you can make to your API, as well as the maximum amount of data transfer allowed. You must opt for the Developer plan or higher; the Consumption plan does not offer sufficient functionality for this integration to work.|
Click Create to begin provisioning your service.
Step 2: Import Your API
For this tutorial, we will be importing and using the Calculator API provided by Microsoft. You can, however, create your own API instead of using the Calculator API.
Launch the API Management service that you created in the previous step.
Open up the Publisher Portal, and click on Import API.
You'll be importing an API from URL.
Set the following parameters:
|Specification document URL||The URL Azure will use to retrieve your API's specification. For this example, use
|Specification format||The API specification format. Use
|New/Existing API||set this to New|
|Web API URL suffix||The value appended to the base URL of your API management service that uniquely identifies the API you're currently creating, such as
|Web API URL scheme||The protocol used to access your API (for this example, set this to
|Products||Add this API to the
When done, click Save to import your API. You'll be redirected to the summary page for your API when it's fully imported.
To use Auth0 to secure your API, you'll need to register Auth0 as an OAuth 2.0 Authorization Server. You can do so using the Azure Publisher Portal.
Navigate to Security > OAuth 2.0.
Click on Add Authorization Server. You'll see the configuration screen that lets you provide details about your Auth0 tenant.
For the purposes of this example, we'll use the Authorization Code grant type, but you're free to use whichever grant type is most appropriate for your use case. Azure currently supports the following grant types: Authorization Code, Implicit, Resource Owner Password, Client Credentials.
Set the following parameters:
|Name||A descriptive name for your authorization server, such as
|Description||A description for your authorization server, such as
|Application registration page URL||The page where users can create or manage their accounts; for the purposes of this example, we'll use
|Authorization code grant types||The grant type used for authorization. Select
|Authorization endpoint URL||The URL Azure uses to make the authorization request. See the Auth0 docs on generating the URL|
|Authorization request method||The HTTP method used by Azure to make the authorization request. By default, this is
|Token endpoint URL||The endpoint used to exchange authorization grants for Access Tokens; Auth0's can be reached at
|Application authentication methods||Method used to authenticate the application; Auth0's is
|Access Token sending method||The location of the Access Token in the sending method (typically the Authorization header)|
|Default scope||Specify a default scope (if necessary)|
Because we're using the authorization code grant, we'll need to provide the client ID and client secret for the Auth0 Application we previously registered. You can find both values in the Application Settings.
Once you've provided both the client ID and client secret, you'll see an auto-generated redirect URI. Copy this URL, since you'll need to provide this URI in your Auth0 Application Settings page in the Allowed Callback URLs section.
When complete, click Save to persist your changes.
Set the Allowed Callback URL
You'll need to provide the redirect URI that was auto-generated during the OAuth 2.0 authorization server setup process to Auth0. Log into the Management Dashboard, and navigate to Applications. Select your Application, and click Settings. Paste the URL into the Allowed Callback URLs field.
Before you can use Auth0 to secure your API, you'll need to set your API to use Auth0. You can do so using the Azure Publisher Portal.
Begin by navigating to the APIs tab, and select the Basic Calculator API.
Click over to the Security tab.
Under User Authorization, select OAuth 2.0. In the new Authorization Server field that appears, select the server you configured in the previous step.
Step 5: Test Your Integration
While logged in to the Azure Portal, open up your instance of the API Management Service. Click Developer Portal to launch the developer-facing side of your APIs.
Go to APIs > Basic Calculator (or the API you've created for this tutorial).
This opens up to the page where you can make a
GET call that allows you to add two integers.
Click Try It. This will bring up the page where you can provide the parameters for your call.
Scroll down to the Authorization section. Next to the Auth0 field, select Authorization Code.
At this point, you'll see the Auth0 login widget in a popup window (if you don't, disable your popup blocker). Provide the credentials for the Auth0 user you created earlier in the tutorial, and sign in.
If you were able to successfully sign in, you'll see a message appear with the expiration date of the Access Token you need to call the API.
Scroll to the bottom, and click Send to send your request. If successful, you'll see a message containing the
HTTP 200 response at the bottom of the page.
In this tutorial, you:
- Configured your Auth0 tenant to act as an OAuth 2.0 server.
- Set up an API Management Service in Azure.
- Imported an API that's managed by Azure's API Management Service.
- Secured your API using Auth0.