I got an access token to fetch the graph client using the following code:
string graphResourceID = "https://graph.windows.net";
string tenantID = ConfigurationManager.AppSettings["ida:Domain"];
string aadInstance = "https://login.microsoftonline.com/" + tenantID +
"/oauth2/v2.0/token";
Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential credential = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientId, appKey);
AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance);
authenticationContext.TokenCache.Clear();
var authResult = await authenticationContext.AcquireTokenAsync(graphResourceID,clientcred);
And then tried to use the token to fetch the signed in User's info via the AD graph api:
Uri servicePointUri = new Uri(graphResourceID);
Uri serviceRoot = new Uri(servicePointUri, tenantID);
ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot,async () => await GetTokenForApplication());
var result = await activeDirectoryClient.Users.Where(u => u.ObjectId.Equals(userObjectID)).ExecuteAsync();
IUser user = result.CurrentPage.ToList().First();
return View(user);
However, this returns the following error:
"error": {
"code": "Authorization_IdentityNotFound",
"message": "The identity of the calling application could not be established.",
"innerError": {
"request-id": "2bdae8ff-d935-4e01-80a1-78cbc8acf4de",
"date": "2017-08-09T18:07:40"
I made sure that mu B2C application has the "Read and Write Directory Data" permission for Windows Active Directory :
Can anyone please help? Been stuck on this for a while. TIA
Edit
I also tried using Microsoft.Graph but end up getting the same error. Plus for B2C users I think it's best if we stick to Azure Ad Graph api for now: https://dev.office.com/blogs/microsoft-graph-or-azure-ad-graph