I am trying to get a list of all office365 users from AzureAD in a web application using below mentioned code. But, authContext.AcquireTokenAsync(resrouce, clientCredential) never returns back the control. I have tried below code for console application and it worked perfectly. But, I am curious to learn why the code does not work for web or what modification do I have to make to make the code work in web.
public static async Task<string> AcquireMyToken()
{
string clientId = "";
string secrect = "";
string resrouce = "https://graph.microsoft.com";
string authority = "https://login.microsoftonline.com/tenanId";
AuthenticationContext authContext = new AuthenticationContext(authority);
ClientCredential clientCredential = new ClientCredential(clientId, secrect);
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resrouce, clientCredential);
return authResult.AccessToken;
}
public static async void ListFiles(string accessToken)
{
var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(
(requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
return Task.FromResult(0);
}));
var users = await graphClient.Users.Request().GetAsync();
}