Please note that here there is no way to set a Key as a Client Secret – why? the explanation is given on the difference between Native app & Web app
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
Application ID – which is the Client ID
Tenant ID => Azure Active Directory > Properties > Directory ID
Go to Azure Active Directory > App registrations > New Application Registration
In the Name field, give a descriptive name
Choose Web app / API
For Sign-on Url:
If you are doing a POC, give a http://localhost:{port number}/
If you are planning to host the code on Azure Web App, then provide as follows: https://{youwebapp}.azurewebsites.net/
Click on Create
Step 2: Configure
Once the App is created, click on Settings
Under Keys, we are going to set the “Client Secret”,
Enter a Key Name (descriptive)
Enter an Expiration Value
On Save, the Client Secret will be generated, take a note of it as it gets hidden once you leave the screen.
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
Application ID – which is the Client ID
Client Secret as per step 10
Tenant ID => Azure Active Directory > Properties > Directory ID
Web app / API – Usage
In this POC – I am getting the Current User Request [me] using GraphServiceClient.
Download the project (use Nuget Manager to download necessary references).
In the GraphController, update ClientId, ClientSecret, TenantId as per above step
Update the UriString as per above step 5
Build and run the code
The entry point is the Gettotken responsible for the Authentication – Access the code using following your local IIS url http://localhost:12345/Graph/Gettoken
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)