Azure Active Directory – Web app / Api – Step by step
Mourtaza Fazlehoussen
Microsoft MVP | Microsoft MCT | Solutions Architect [M365 - Power Platform] | IT Security| OpenAI
In continuation of my previous blog – Register an app with the Azure Active Directory v2.0 endpoint – demonstrating how to create an Application Type: Web App /API within Azure.
Create Web app / API
Step 1: Create
6. Click on Create
Step 2: Configure
Now Under Required permissions, based on all available API, set all necessary permissions you need to, please note here that after settings up permissions, you/AAD Admin need to “Grant” them explicitly otherwise it will not work.
Step 3: Take Note
Web app / API – Usage
In this?POC ?– I am getting the Current User Request [me] using GraphServiceClient.
领英推荐
A. Get Authorization Code (see the solution for complete code)
AuthenticationContext authContext = new AuthenticationContext(authorityURL, true);
Task redirectUri = authContext.GetAuthorizationRequestUrlAsync(resource, clientId, new Uri(uriString), UserIdentifier.AnyUser, string.Empty);
redirectUri.Wait();
return Redirect(redirectUri.Result.AbsoluteUri);
Please note here that the AbsoluteUri has to match with the UriString otherwise it won’t work – this is an extra layer of security added by Microsoft.Once successful, it will redirect to the Gettoken method once more to get the access token.
B. Use Authorization Code to request the Access Token (see the solution for complete code)
string code = Request.Params["code"];
ClientCredential clientCredentials = new ClientCredential(clientId, clientSecret);
Task request = authContext.AcquireTokenByAuthorizationCodeAsync(code, new Uri(uriString), clientCredentials);
request.Wait();
Session["code"] = request.Result.AccessToken;
return RedirectToAction("Index");
Once successful, it will redirect to the Index method for further processing.
C. Use Authorization Code to request the Access Token (see the solution for complete code)
public ActionResult Index(string authenticationCode)
{
string code = (string)Session["code"];
GraphServiceClient graphClient = GetGraphClient(code);
//Get User information [me]
Task meRequest = graphClient.Me.Request().GetAsync();
meRequest.Wait();
User resultMeRequest = meRequest.Result;
Response.Write(resultMeRequest.AboutMe);
return View();
}
As a summary, we continued our journey with Azure Active Directory v2.0 endpoint, focusing on creating and configuring a Web App / API. The process is straightforward:
The post also provides a detailed walkthrough of using the Web App / API in a POC, explaining how to get the current user request using GraphServiceClient, and how to update, build, and run the code. It covers the process of getting the authorization code, using it to request the access token, and using the access token for further processing. The code snippets provided guide you through each step of the process.
Stay tuned for more insights on working with Azure!