The following topics will be covered in this post.
- Create APIM
- Create API
- App registration in Azure AD
- Configure APIM to use OpenId Connect (Create Authorization Server)
- Configure Reply URLs for Developer Portal and Prod App
- Configure API to use OpenId connect
- Test using Developer Portal
- Test using MVC Client Application
It is assumed that you are having an Azure subscription with access to Azure AD in the tenant.
Step 1 - Create APIM

Step 2
Step 3
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using Swashbuckle.Swagger.Annotations;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Microsoft.Azure.ServiceBus;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using WebApplication5.Models;
- namespace WebApplication5.Controllers {
- public class ValuesController: ApiController {
- const string ServiceBusConnectionString = "Endpoint=sb://yiintergration.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ep+4iaM1XDgl0zVcnDSENHSi05CWtrTSsxvuKpfFy0c=";
- const string QueueName = "eyiintegration";
- static IQueueClient queueClient;
- // POST api/values
- [SwaggerOperation("Create")]
- [SwaggerResponse(HttpStatusCode.Created)]
- public string Post(WorkProducts w) {
- const int numberOfMessagesToSend = 1;
- queueClient = new QueueClient(ServiceBusConnectionString, QueueName);
- try {
- int Count = w.result.Count;
- for (var i = 0; i < Count; i++) {
- string workProduct = JsonConvert.SerializeObject(w.result[i]);
- // Create a new message to send to the queue.
- //string messageBody = workProduct..ToString();
- var message = new Message(Encoding.UTF8.GetBytes(workProduct));
- // Write the body of the message to the console.
- //Console.WriteLine($"Sending message: {messageBody}");
- // Send the message to the queue.
- queueClient.SendAsync(message);
- }
- } catch (Exception exception) {
- Console.WriteLine($ "{DateTime.Now} :: Exception: {exception.Message}");
- }
- return "Successfully received WorkProduct and sent to message queue for further processing ";
- }

Step 5

Step 6

Step 7

Step 8
- {
- "appId": "191a48ca-9f41-47c1-a9f4-15c979971df8",
- "appRoles": [],
- "availableToOtherTenants": true,
- "displayName": "testapimab",
- "errorUrl": null,
- "groupMembershipClaims": null,
- "optionalClaims": null,
- "acceptMappedClaims": null,
- "homepage": "https://testapimab.portal.azure-api.net",
- "informationalUrls": {
- "privacy": null,
- "termsOfService": null
- },
- "identifierUris": ["https://aswinbhaskaranabtechnet.onmicrosoft.com/998c1447-8067-48b1-a4b3-c7dee1fd81b4"],
- "keyCredentials": [],
- "knownClientApplications": [],
- "logoutUrl": null,
- "oauth2AllowImplicitFlow": true,
- "oauth2AllowUrlPathMatching": false,
- "oauth2Permissions": [

Step 10

Step 11
The Metadata Endpoint URL will be your Azure AD Metadata Endpoint URL. Please make sure to replace the underscores with your Azure TenantId.
https://login.microsoftonline.com/c683b381-c32e-4bc5-926c-a0a9371a336f/.well-known/openid-configuration

Step 12 - Configure Redirect URI

Step 13

Step 14

Step 15

Step 16

Step 17

Step 18

- Open-Config-url should be Azure AD Metadata URL and the highlighted should be replaced with the Tenant Id. Refer to Step 11.
- Aud claim value should be APIM Client Id from App registration. Refer to Step 5
- <policies>
- < inbound>
- < 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/c683b381-c32e-4bc5-926c-a0a9371a336f/.well-known/openid-configuration” />
- < required-claims>
- < claim name=”aud”>
- < value>191a48ca-9f41-47c1-a9f4-15c979971df8</value>
- < /claim>
- < /required-claims>
- < /validate-jwt>
- < base />
- < /inbound>
- < backend>
- < base />
- < /backend>
- < outbound>
- < base />
- < /outbound>
- < on-error>
- < base />
- < /on-error>
- < /policies>
Step 20

Step 21

Step 22
- <add key="UnobtrusiveJavaScriptEnabled" value="true" />
- <add key="ida:ClientId" value="191a48ca-9f41-47c1-a9f4-15c979971df8" />
- <add key="ida:AADInstance" value="https://login.microsoftonline.com/" />
- <add key="ida:AADInstanceLogin" value="https://login.windows.net/{0}" />
- <add key="ida:ClientSecret" value="d3gqg85ro4MV84P+XS5DYhupwRfloJFHoLENmlbQBuA=" />
- <add key="ida:AppKey" value="d3gqg85ro4MV84P+XS5DYhupwRfloJFHoLENmlbQBuA=" />
- <add key="ida:Domain" value="aswinbhaskaranabtechnet.onmicrosoft.com" />
- <add key="ida:Tenant" value="aswinbhaskaranabtechnet.onmicrosoft.com" />
- <add key="ida:TenantId" value="c683b381-c32e-4bc5-926c-a0a9371a336f" />
- <add key="ida:PostLogoutRedirectUri" value="https://localhost:44346/" />
The initial request to get the Authorization Code from OpenId Provider will be called from Configure App method in Startup.Auth.cs. And the Authorization Code wilAuthorization Code will be cached using ADAL.cs. And the Access Token will be acquired when GetTokenForBackendApplication() method is called. Please make sure to update the Subscription key within GetAPIMDemoValues() method.

Step 24 - JWT(Access Token)


Step 26

Conclusion
Sandeep KPosted Apr 17, 2019, 3:07 PM
For any client api to consume api s published through APIM , should the client api have registration in azure ad?