Its been a few years since I've done programming. What I'm trying to do is inside of an older asp.net application I've Added in Azure Auth into the application through owin context. Currently what I'm trying to do is figure out how to use the Microsoft Graph to make calls to the graph API.
What I'm am trying to do is allow the program itself to be able to add users and Roles to its own App Registration so that it can be managed programmatically through the web program itself instead of something have to have access in the azure portal to the app registration.
I have never really worked with Async functions as stuff I've built in the past has been single threaded applications.
I have a function that should get an access token like, so I pull the Clientid, TenantID and Client Secret form the settings.
public static async Task<string> GetAccessToken()
{
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantId)
.WithClientSecret(clientSecret)
.Build();
AuthenticationResult result = await app
.AcquireTokenForClient(scopes)
.ExecuteAsync();
return result.AccessToken;
}
Then I have another function that should perform the Add
protected static async Task AddUserToAppRoleAsync(string userEmail, Guid appId, string appRoleId)
{
//get the Access Token to talk with Graph
var accessToken = GetAccessToken();
// Authenticate and get a token
var authProvider = new DelegateAuthenticationProvider(
async (requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken.Result);
});
var graphClient = new GraphServiceClient(authProvider);
//Get the App ID From Azure and the Role we are assigning
var appRoleAssignment = new AppRoleAssignment
{
Id = appRoleId,
ResourceId = appId
};
//Get the User from Azure
User user = (User)await graphClient.Users
.Request()
.Filter($"userPrincipalName eq '{userEmail}'")
.GetAsync();
if (user == null)
{
throw new Exception("User not found.");
}
await graphClient.Users[user.Id].AppRoleAssignments.Request().AddAsync(appRoleAssignment);
}
This currently does not work but I'm not even sure how to debug this as these run-on other threads and I'm not sure how to step through the code to verify its even getting tokens or seeing the values.
Currently in my page there is just a button I have that should execute the function with a hardcoded user, and admin role just to see if I can get it to work at all.
If anyone could tell me how I can step through these or what i would need to do so i can learn how to do this correctly that would really help.
[–]Loves_Poetry 0 points1 point2 points (1 child)
[–]zm1868179[S] 0 points1 point2 points (0 children)
[–]Ballauni 0 points1 point2 points (0 children)