A free and open-source web framework that enables developers to create web apps using C# and HTML, developed by Microsoft.
I used client credential flow in Blazor server application, this is my code. You can also use this code in .net core MVC/API applications. The nuget package I installed is <PackageReference Include="Microsoft.Graph" Version="4.43.0" /> Please pay attention to the version, the lateset version shouldn't be V4.x and using the latest version would require modify some of the code.
@page "/counter"
@using Azure.Identity;
@using Microsoft.Graph;
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
<button class="btn btn-primary" @onclick="getUser">List users assigned to the enterprise application </button>
<input type="text" value="@userids" />
@code {
private int currentCount = 0;
private string userids = "null";
private void IncrementCount()
{
currentCount++;
}
private async Task getUser()
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "xxxx";
var clientId = "xxx";
var clientSecret = "xxx";
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var aa = await graphClient.ServicePrincipals["object_id"].AppRoleAssignedTo.Request().GetAsync();
userids = "";
for (var i = 0; i < aa.Count; i++)
{
if (aa.CurrentPage[i].PrincipalType == "User")
{
userids += aa.CurrentPage[i].PrincipalId + "===";
}
}
}
}
================================================
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
TinyWang