I have a .net core api and User.Read delegated permission is given.
Authentication section in startup.cs:
services.AddAuthentication("Bearer")
.AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAd"));
Getting the graph api client:
var credential = new DefaultAzureCredential();
var token = credential.GetToken(
new Azure.Core.TokenRequestContext(
new[] { "https://graph.microsoft.com/.default" }));
var accessToken = token.Token;
_graphServiceClient = new GraphServiceClient(
new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
return Task.CompletedTask;
}));
Not sure if i need to have application type permission and use user/{userID}. But why i don’t understand. Getting error: me request is only valid with delegated authentication flow .
2
Answers
These are the permissions required to access the user details using the both delegated and application permissions:
As far as for /me endpoint as mentioned in the note here:
Hope this helps.
You are defining delegated permission but passing token within it. Have a look on the below document:
First let me clarify, when delegated permission required. If you want to access UserList from your application its called application permission then you would need to pass auth token. But if you want to access UserList while the user login you need delegated permission auth token wouldn’t need to pass there. Therefore, as you are mixing them consequently encountered that perticular error.
Please check the details steps here.
Right Authentication Provider:
Based on your scenario, you can call Graph API using numerous authentical protocol. For instance, authorization code flow enables native and web apps to securely obtain tokens in the name of the user. You can implement as following:
Note: More details can be found here
Using Graph SDK:
Note: You can check here
Using Token aquisition service:
Program.cs Configuration:
You can check details here.
Note: If you still need more information, you could check our official document here.