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

3 answers

Sort by: Newest
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    2 deleted comments

    Comments have been turned off. Learn more

  2. 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?


  3. 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.