Azure Active Directory – Native app – Step by step
Mourtaza Fazlehoussen
Senior IT Manager | Microsoft MVP | Microsoft MCT | Solutions Architect [M365 - Power Platform] | IT Security | AI Futurist
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 a Native app
Step 1: Create
Step 2: Configure
Step 3: Take Note
领英推荐
Native App – Usage
Here the code is straightforward:
A. Get Access Token
public static string GetAccessToken()
{
string AppId = "";
string TenantId = "";
string GraphResourceUrl = "https://graph.microsoft.com";
string AuthorityUrl = "https://login.microsoftonline.com/" + TenantId;
string RedirectUri = "https://localhost:12345/";
try
{
AuthenticationContext authContext = new AuthenticationContext(AuthorityUrl, true);
AuthenticationResult authResult = authContext.AcquireTokenAsync(GraphResourceUrl, AppId, new Uri(RedirectUri), new PlatformParameters(PromptBehavior.Auto)).Result;
return authResult.AccessToken;
}
catch (Exception ex)
{
}
return null;
}
B. Get GraphServiceClient
public static GraphServiceClient GetGraphClient(string graphToken)
{
try
{
DelegateAuthenticationProvider authenticationProvider = new DelegateAuthenticationProvider(
(requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", graphToken);
return Task.FromResult(0);
});
return new GraphServiceClient(authenticationProvider);
}
catch (Exception ex)
{
}
return null;
}
This blog post continues from a previous one about registering an app with the Azure Active Directory v2.0 endpoint. It demonstrates how to create a Native app within Azure. The process involves three main steps:
The blog also provides code for using the Native App. It includes a method to get an Access Token and another to get a GraphServiceClient using the obtained token. The Access Token is acquired using the AppId, TenantId, GraphResourceUrl, AuthorityUrl, and RedirectUri. The GraphServiceClient is obtained using the graphToken and a DelegateAuthenticationProvider.