In my ASP.NET MVC Core project, I want all the AppRoles that are set up in the App Registration as a list to display in my view.
Currently I have attempted the following:
var servicePrincipal = await _graphServiceClient.Applications[_configuration["AzureAd:AppRegistrationId"]]
.Request()
.Select("appRoles")
.GetAsync();
I have granted the application the API permissions Application.Read.All
and Directory.Read.All
, but I still get error:
ServiceException: Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation
Questions:
- Am I even on the right path to achieve what I want and just missing the correct permission?
- Does anyone know what permission is required then?
- Is there a different way to achieve this task?
- I have attempted getting the roles out of the claim, but that only displays the user’s roles, not all the roles that are set up on the App Registration
Thank you in advance for any assistance!
3
Answers
While this is not a direct answer to my original question, I ended resolving the issue by using a different approach.
I used the package Microsoft.Graph.Auth to generate a credential, then connect to Graph with that credential (basically connect as my Application Registration). Here's the function:
Probably not the best way, but works, so I am happy.
Based on the documentation
here
,Application.Read.All
is the right permission to read information about an application.I believe the reason you are getting this error is because you have assigned this permission to your application but it seems the user who’s making the Graph API request does not have that permission.
Please check the permissions assigned to the user who’s making the request and make sure that they have the appropriate permissions to make this request.
looks like I’m late…
I test with delegated permission and application permission, both of them are ok.
for application permission, it would be easy to do it with code like below:
This requires application type of api permission:
If we want to use the delegated api permission, since this is an MVC application, we need to integrate AAD authentication into the application first. We can follow this sample. To generally speaking, add codes in program.cs like this:
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApp(builder.Configuration) .EnableTokenAcquisitionToCallDownstreamApi() .AddMicrosoftGraph(builder.Configuration.GetSection("DownstreamApi")) .AddInMemoryTokenCaches();
Then inject
graphclient
into the code and use it call graph api,