Reputation: 3495
I am using .NET Core
with C#
application and there I have integrated the Azure AD authentication. It means any Azure AD user can be logged in to this application. Now at someplace, I want to display the City, State, Country and some other fields value of current logged in user. For that, I have used Microsoft Graph API, but I am not able to get the details successfully. I got an error Request_ResourceNotFound
. I used below C#
code to get the data for current Logged in user.
var clientId = "<CLIENT_ID>"; // I have used my application client id here
var secret = "<CLIENT_SECRET>"; // I have used my client secret here
var domain = "<DOMAIN>"; // I have used domain here
var credentials = new ClientCredential(clientId, secret);
var authContext = new AuthenticationContext($"https://login.microsoftonline.com/{domain}/");
var token = await authContext.AcquireTokenAsync("https://graph.microsoft.com/", credentials);
var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("bearer", token.AccessToken);
return Task.CompletedTask;
}));
var user = await graphServiceClient
.Me
.Request()
.GetAsync(); // Error at this line
But it throws an below error when I used graphServiceClient.Me.Request().GetAsync()
Error: Code: Request_ResourceNotFound
Message: Resource 'XXXX-XXXX-XXX-XXXX' does not exist or one of its queried reference-property objects are not present.
Have anyone faced this kind of error earlier, Can anyone suggest how to get the data for currently logged-in User using Microsoft Graph in C#?
Upvotes: 2
Views: 2244
Reputation: 33094
You're using Client Credentials so there isn't a "currently logged-in User". It is actually the entire point of Client Credentials, authenticating an application without a user.
When using Client Credentials, you need to explicitly specify the User id
or userPrincipalName
:
var user = await graphServiceClient
.Users["[email protected]"]
.Request()
.GetAsync();
Upvotes: 5