Hi folks , I am writing this blog to demonstrate step by step procedure for creating simple Web Api and using it with Azure ADAL.

Step 1: Open Visual Studio and Create a new ASP.Net Web Application. Click Ok

Step 1.PNG

Step 2: In the Next Window Select Web API template as shown below and Click Ok

Step 2.PNG

Step 3: Now your sample Web API have been created successfully. By Default you can see three controllers in your project.

In my case i am going to create a new Controller and name it as "DynamicsController.cs"

Step 3.PNG

Step 4.PNG

Click Add. Next you can define Routing as our requirement. But as of now i am using default routing . Reference : Web API Routing

Step 5.PNG

Step 4: We have created Sample Web API which will return "success" as a string. Now we have to publish it in Azure. Right Click on the project and Click Publish

Step 6.PNG

But before publishing make sure you have the Azure Subscription.

Step 7.PNG

Select Azure Web Apps and Click Next. Select the required Azure Subscription and Resource Group and Click Publish .

Now the Web Api App have been successfully Hosted In Azure.

Step 5: Open portal.azure.com . Navigate to App Service you can see the hosted App in the list.

Step 8.PNG

Step 6: Click and Open the App you Hosted. You can see list of Ribbon Buttons, Scroll Down to Authentication/Authorization.

On Right Side Turn On App Service Authentication -> Select Login with Azure Active Directory.  Refer Below Screen shots.

Step 9.PNG Step 10.PNG

Step 7: Now Comes the important steps to follow. (Step were most developers lose their path)

We have enabled Azure Authentication, Now we have to create App in Azure Active Directory to access our Web Api.

Issue: Most of the blogs i googled are like create a single AAD App as type Web app and use it for consuming Hosted Web Api. But in most cases it wont work as expected.

So Follow the steps carefully.

Important: Create two AAD Apps with Application Type as Web App/Api.

  • Select Azure Active Directory in Authentication Provider
  • Step 11.PNG
  • In the next window select Express as shown Below
  • Step 12.PNG
  • Select Create New AD App and populate the Name in Create App TextBox and Click Ok and Save.
  • Now we have successfully created App to access Custom Web Api (Assume it as Parent App).
  • Navigate to Azure Active Directory -> Click App Registration.
  • In the List you can see the App which we created before.
  • Step 13.PNG
  • Open the App and Copy the Client Id, which is required while consuming our web api.
  • Application Id is the Client Id here.

  • Step 14.PNG
  • Navigate to (Settings-> Owners) Check whether you are the owner of the App.
  • Navigate to (Settings-> Required Permission) By default you will have Delegate permission for Window Azure Active Directory API .

Step 8: Next we have to create a new App in AAD for accessing the App which we created before. (Assume this is Client Side App i.e used to access our Parent AAD App)

  • Navigate to Azure Active Directory -> Click New App Registration.
  • Step 15.PNG
  • Select Application Type as Web app/API as shown below.
  • Step 16.PNG
  • Click Create.

Step 9: Now we have created two Apps in AAD. (Assume First one as Parent and Second one as Client)

Open the client App. Navigate to Settings->Required Permission.

Step 17.PNG

Click Add as Shown.

Step 18.PNG

Select API and Search for Parent App and Select the app and click ok.

In the next window Allow Delegate Permission for the App as shown below.

Step 19 a.PNG

Step 19.PNG

Grant Permission as show below.

Step 20.PNG

Step 10: Now we have to create Client Secret Key for the Client App.

Navigate to Keys-> Create New Secret Key as shown Below and Click Save.

Once saved the Secret key will be visible copy the key.

Step 21.PNG

Step 22.PNG

Note: Client Id(Application Id) and Client Secret Key will be used to authenticate our Web Api in code while consuming.

SUCCESS:  we have successfully created Web API App and Hosted in Azure with ADAL. I will write next blog to demonstrate how to consume Custom Web Api with Azure ADAL in CRM Plugin.

Overview.

  1. Created a new Web API App.
  2. Hosted in Azure.
  3. Created two App in AAD (Parent and Client).
  4. Authenticated Web API App with Parent App AAD.
  5. Allow permission to Client AAD App by adding Parent AAD App.
  6. By using Client AAD App(Client Id & Client Secret Key) we can consume our hosted Web Api.

Hope it's helpful. Happy Coding. #Herbi_Coder