Azure Active Directory Graph Api allows you to perform operations on the signed in user.
https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/signed-in-user-operations
I don't know how to obtain an access token for Graph API on behalf of the signed in user.
I can obtain an access token for my web application, but this is not on behalf of a particular user:
var authContext = new AuthenticationContext(authorityString);
var result = await authContext.AcquireTokenAsync
(
"https://graph.windows.net",
clientCredential // Application ID, application secret
);
string accessToken = result.Token;
Also, when the user logs on I am given an Authorization Code (via the AuthorizationCodeReceived notification). I can convert this Authorization Code to an access token. This might be an access token for the user, but it is not recognized by Graph Api. (Come to think of it, I don't know what this access token is good for...)
var authContext = new AuthenticationContext(authorityString);
var result = await authContext.AcquireTokenByAuthorizationCodeAsync
(
authorizationCode,
redirectUri, // eg http://localhost:56950/
clientCredential // Application ID, application secret
);
string accessToken = result.AccessToken;
Using the first access token, I can run queries on my Active Directory tenant. For example, the call https://graph.windows.net/thisisnotmydomain.onmicrosoft.com/users?api-version=1.6
gives me a list of users.
But if try the URL: https://graph.windows.net/me?api-version=1.6
, I get this result:
{
"odata.error": {
"code": "Request_ResourceNotFound",
"message": {
"lang": "en",
"value": "Resource not found for the segment 'me'."
}
}
}
Which makes sense, as I never specified the user. If I use the access token obtained by AcquireTokenByAuthorizactionCodeAsync
I receive an error message "Access Token missing or malformed." Clearly, this is not a graph API access token.
How do I get an access token for Graph API which works for the signed in user?