How Can I get all the users who has a certain role in Azure AD App Registration Using Dotnet/Blazor?

Mohammed Waseem 20 Reputation points
2023-08-23T15:48:52.19+00:00

I have an app which uses Azure AD App registration for user and roles management (Access controlled based on Roles assigned). I want to list down all the users who has a certain role in this enterprise application registration. Can anyone help me with the code for Blazor? Thank you!

Developer technologies | .NET | Blazor
Microsoft Security | Microsoft Entra | Microsoft Entra ID
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author
Anonymous
2023-08-29T10:22:31.85+00:00

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

Was this answer helpful?

1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Marilee Turscak-MSFT 37,426 Reputation points Microsoft Employee Moderator
    2023-08-23T21:50:46.4866667+00:00

    @Mohammed Waseem ,

    Here is a Blazor example from the official documentation for retrieving the name of the user's first role:

    
    <AuthorizeView>
        <Authorized>
           <p>@context.User.Claims.FirstOrDefault(c=>c.Type == "role").Value</p>
        </Authorized>
    </AuthorizeView>
    

    The best way to retrieve the role is to get it from the roles claim in the JWT token (http://schemas.microsoft.com/ws/2008/06/identity/claims/role ).

    In your authorization code, it is recommended to check whether a particular claim value is present, rather than assuming the user has just one role claim.

    if (context.User.HasClaim(ClaimTypes.Role, "Admin")) { ... }

    See related threads:

    https://stackoverflow.com/questions/60402678/how-to-set-find-user-roles-from-organization-based-identity-in-net-core

    https://stackoverflow.com/questions/60007447/blazor-authentication-with-azure-ad-retrieving-user-roles

    Let me know if this is close to what you are looking for. We don't have an exact sample for this.

    If the information helped you, please Accept the answer. This will help us as well as others in the community who may be researching similar questions. Otherwise let us know if you have further questions.

    Was this answer helpful?


  2. Bruce (SqlWork.com) 85,786 Reputation points
    2023-08-23T17:23:10.9033333+00:00

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.