{"id":515,"date":"2018-04-12T13:53:26","date_gmt":"2018-04-12T13:53:26","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/aaddevsup\/?p=515"},"modified":"2019-11-05T17:02:45","modified_gmt":"2019-11-05T17:02:45","slug":"implementing-service-to-service-authorization-and-getting-the-access-token-with-postman-utilizing-client-credential-grant-type","status":"publish","type":"post","link":"https:\/\/blogs.aaddevsup.xyz\/2018\/04\/implementing-service-to-service-authorization-and-getting-the-access-token-with-postman-utilizing-client-credential-grant-type\/","title":{"rendered":"Implementing Service to Service Authorization and Getting the Access Token with Postman Utilizing Client Credential Grant Type"},"content":{"rendered":"

Introduction<\/h1>\n

This article is meant to show how one can set up a client application to obtain a service to service access token, to get access to a web API from a web App. This document will be following the grant type client credential flow to do this, and will utilize Postman to get the access token via client credentials. This tutorial will not set up the backend web API, and assumes that web API is validating the token, you can click this link on validating the JWT token here.<\/a> Also in addition to that, the link included assumes that the Audience has the correct permissions. As the client credentials flow can get a different audience within your tenant, the end of this article will review what additional steps, to make sure that the access token has the correct permissions.<\/p>\n

 <\/p>\n

<\/h1>\n

Creating the AAD Application Registrations in the Portal<\/h1>\n

First we are going to want to create the AAD Application registrations in the portal. For this we will implement the application to be able to work with Postman so that we can display getting the access token pretty easily. First we go to the Azure Active Directory <\/em>Blade, go to App Registrations, <\/em>and then create a new application registration<\/em>.<\/p>\n

 <\/p>\n

 <\/p>\n

<\/h1>\n

\"2018-03-31<\/a><\/p>\n

 <\/p>\n

From there we are going to want to create a web app with any name. Here I have set the name as web app<\/em> and then we want to set the callback url to : https:\/\/www.getpostman.com\/oauth2\/callback<\/a> and set the application type to web app\/ API.<\/p>\n

 <\/p>\n

\"image\"<\/a><\/p>\n

 <\/p>\n

You will have to click out of the sign-on URL to make it check whether or not if it\u2019s correct.<\/p>\n

 <\/p>\n

After that we have created our web app, we will want to create a secret. Please keep track of the secret as you won\u2019t be able to see the secret again. You will have to press save in order for the secret to generate.<\/p>\n

 <\/p>\n

\"image\"<\/a><\/p>\n

 <\/p>\n

With this information in hand, we will be able to move forward and connect to this AAD registration. But without the correct permissions we won\u2019t be able to connect to the web API registration with an access token from this web API.<\/p>\n

 <\/p>\n

\"image\"<\/a><\/p>\n

 <\/p>\n

So here I create the API registration, with the sign-on URL as the localhost. How you plan on setting up your application will change what the sign-on URL will be.<\/p>\n

 <\/p>\n

 <\/p>\n

Modifying the Application Manifest to Add Your Own Permissions<\/h1>\n

Since we are utilizing Client Credentials, we will need to add the permission in the Web API as an Application Permission as the Client Credential doesn\u2019t utilize the delegated permissions. In addition to that we will most likely want to define what sort of permissions the web app should have for the web API. In order to do this we will have to add these permissions directly into the web API\u2019s application manifest. You can do this by going to the AAD Application registration, clicking on Manifest<\/em> and then adding the field \u201capproles\u201d into it. This is shown below.<\/p>\n

\"\"<\/b><\/i><\/u><\/sub><\/sup><\/span><\/p>\n

<\/b><\/i><\/u><\/sub><\/sup><\/span><\/p>\n

The approle I added here is below. By setting the allowedmembertypes to \u201cApplication\u201d we are setting it as an application role.<\/p>\n

 <\/p>\n

“appRoles”: [
\n{
\n“allowedMemberTypes”: [
\n“Application”
\n],
\n“displayName”: “Test Application Permission”,
\n“id”: “3ea51f40-2ad7-4e79-aa18-12c45156dc6a”,
\n“isEnabled”: true,
\n“description”: “I am a Test Application Permission.”,
\n“value”: “Test.Application.Permission”
\n}
\n]<\/em><\/p>\n

 <\/p>\n

To learn more about these values you can go to the link here describing the application manifest :<\/p>\n

https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/develop\/active-directory-application-manifest<\/a><\/p>\n

 <\/p>\n

To learn more about the App roles for an application please go here:<\/p>\n

https:\/\/docs.microsoft.com\/en-us\/azure\/architecture\/multitenant-identity\/app-roles<\/a><\/p>\n

 <\/p>\n

That being said, the one thing to note is that the allowedmembertypes<\/em> isn\u2019t described fully in the documentation above. By setting it to User, we set it as a delegated permission and have to ability to do RBAC access with the delegated permission. By setting it to an Application, we are able to set it as an application permission, letting anyone who can access the application to be able to access web API with the permissions granted to the web app.<\/p>\n

 <\/p>\n

You can learn more about the differences between delegated and application permissions here.<\/a><\/p>\n

 <\/p>\n

If you are interested in utilizing RBAC to control access in a service to service scenario you can follow this guide :<\/p>\n

https:\/\/azure.microsoft.com\/en-us\/resources\/samples\/active-directory-dotnet-webapp-roleclaims\/<\/a><\/p>\n

 <\/p>\n

In addition to that, the ID is created by utilizing the method below in PowerShell.<\/p>\n

[System.Guid]::NewGuid()<\/p>\n

 <\/p>\n

Adding the Permission into the Web App<\/h1>\n

After adding the application permission in the application manifest, we are able to add the required permission to the web app in order to get an access token with the correct claims saying that this web app has application access to the web api.<\/p>\n

\"image\"<\/a><\/p>\n

 <\/p>\n

 <\/p>\n

\"image\"<\/a><\/p>\n

 <\/p>\n

\u00a0<\/a><\/p>\n

Now we have the Application permission and we will be able to get it in the claims when we authenticate with the web app. Make sure to grant permissions, otherwise you won\u2019t see the role in the access token as the application technically hasn\u2019t been given permission yet as it requires a global admin to give the application access to the permission. Notice how it says yes under require admin in the last picture.<\/p>\n

 <\/p>\n

 <\/p>\n

\"image\"<\/a><\/p>\n

 <\/p>\n

 <\/p>\n

 <\/p>\n

 <\/p>\n

Getting the Access Token with Postman<\/h1>\n

 <\/p>\n

After granting permissions for the application, we are now able to authenticate with the application and get the correct claims and audience in the access token. We will utilize client credentials to get an access token from Web App\u2019s application ID with the secret and then with the resource set as the app ID URI for the web API, we will get the audience as the app id uri, and the roles for whatever permissions were granted to the web app.<\/p>\n

 <\/p>\n

\"image\"<\/a><\/p>\n

 <\/p>\n

 <\/p>\n

 <\/p>\n

\"image\"<\/a><\/p>\n

 <\/p>\n

 <\/p>\n

 <\/p>\n

\"image\"<\/a><\/p>\n

 <\/p>\n

\"image\"<\/a><\/p>\n

 <\/p>\n

From here, you can access the web api, assuming that you have set it up to authorize based on the audience, role, iss, and signature is correct (i.e. the access token hasn\u2019t been tampered with and it is issued from the AAD tenant with the AAD Application registration in it). For more information on validating a JWT token you can find that here.<\/a><\/p>\n

 <\/p>\n

Conclusion<\/h1>\n

 <\/p>\n

<\/h1>\n

<\/h1>\n

<\/h1>\n

<\/h1>\n

We have now gone through all the steps to set up the AAD application registration, modify the application manifest in the web API to create the permissions, add permissions in the web app aad application registration, grant permissions, and get the Access token using Postman to see if all the claims in the JWT token are correct. After all of this your web api will need to still manually validate that the jwt token hasn\u2019t been tampered with and that all the claims are correct. After doing all these steps you should have now setup a service to service authorization using client credentials.<\/p>\n","protected":false},"excerpt":{"rendered":"

Introduction This article is meant to show how one can set up a client application to obtain a service to service access token, to get access to a web API from a web App. This document will be following the grant type client credential flow to do this, and will utilize Postman to get the access token via client credentials. This tutorial will not set up the backend web API,…<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5,6,10],"tags":[24,45,53,86,114,126],"class_list":["post-515","post","type-post","status-publish","format-standard","hentry","category-app-registration","category-azure-ad","category-azure-ad-graph","category-microsoftgraph","tag-access-token","tag-client-credential-grant","tag-create","tag-how-to-implement-aad-application","tag-postman","tag-service-to-service"],"_links":{"self":[{"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/posts\/515"}],"collection":[{"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/comments?post=515"}],"version-history":[{"count":0,"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/posts\/515\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/media?parent=515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/categories?post=515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/tags?post=515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}