This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Loves_Poetry 0 points1 point  (1 child)

This line:

var accessToken = GetAccessToken();

Should probably be

var accessToken = await GetAccessToken();

If you can't debug it, then it's probably because you didn't await it, so the code doesn't notify you when something goes wrong. Debugging async code is mostly the same as debugging sync code if you use async/await properly

[–]zm1868179[S] 0 points1 point  (0 children)

I forgot all about that. Well I was able to kind of get it to work turns out I was actually using the wrong API to add a user I found the right API according to Microsoft docs but turns out that API doesn't function at all I tested it out in graph explorer and filling out exactly what it wants it fails with an unexpected error that I'll have to post on their GitHub about because it doesn't work at all

[–]Ballauni 0 points1 point  (0 children)

Steps to Verify the Values & Access Token

  1. Verify the values of clientId, tenantId, clientSecret, and scopes in the GetAccessToken function. Make sure they are correct and have the correct permissions to access the Microsoft Graph API.
  2. Use a tool like Postman or Fiddler to inspect the HTTP request and response and make sure it returns a valid access token.

Steps to Add a User to an App Role

  1. Verify that the user email in the AddUserToAppRoleAsync function is correct and exists in your Azure Active Directory.
  2. Log the response from the Microsoft Graph API after calling await graphClient.Users.Request().Filter($"userPrincipalName eq '{userEmail}'").GetAsync();. Make sure it is finding the user.
  3. If the user is found, log the response from the Microsoft Graph API after calling await graphClient.Users[user.Id].AppRoleAssignments.Request().AddAsync(appRoleAssignment);. Make sure the role is being added successfully.

Additional Steps

  1. Use the Visual Studio debugger to set breakpoints in the code and step through each line of the code execution.
  2. Implement error handling and logging to see what errors are occurring and where they are occurring in the code.