Get all users with a given role

I'm trying to write a new method on ServerMethodsController.Custom.cs that needs to fetch all users with a given role, but I'm having a hard time figuring it out. How can this be achieved?

How can I access the userManager and roleManager from a method on ServerMethodsController.Custom.cs?

You can inject them:

namespace Sample.Controllers
{
    [Route("api/[controller]/[action]")]
    public class ServerMethodsController : Controller
    {
        private readonly UserManager<ApplicationUser> userManager;
        private readonly RoleManager<IdentityRole> roleManager;

        public ServerMethodsController(UserManager<ApplicationUser> userManager,  RoleManager<IdentityRole> roleManager)
        {
            this.userManager = userManager;
            this.roleManager = roleManager;
        }

        public async Task<IEnumerable<ApplicationUser>> GetUsersForRole(string role)
        {
           return await this.userManager.GetUsersInRoleAsync(role);
        }
    }
}

1 Like

Nice. What else can be injected? I didn't find anything about that in the docs.

However, in reality, I needed that in an Entity controller trigger (OnMyEntityCreated), but when I create another constructor in the MyEntityController.Custom.cs partial class and run the app, it throws an exception: Multiple constructors accepting all given argument types have been found in type 'MyApp.Controllers.MySql.MyEntityController'. There should only be one applicable constructor.

public partial class OrcamentosController
{
    private readonly UserManager<ApplicationUser> userManager;
    private readonly RoleManager<IdentityRole> roleManager;

    public MyEntityController(MySqlContext context, UserManager<ApplicationUser> userManager,  RoleManager<IdentityRole> roleManager) : this(context)
    {
        this.userManager = userManager;
        this.roleManager = roleManager;
    }

Documentation about injecting is available here.

What else can be injected?

You can inject anything that is provided by ASP.NET Core. You can find more info about dependency injection here.

Not sure about the runtime exception. Have you tried looking for it online?

1 Like

Yes, but I couldn't find an answer...
I wasn't familiar with ASP.NET Core dependency injection and still don't fully understand it.

I get no troubles creating a constructor with all the dependencies I need in a ServerMethodsController. But what I really need is to inject those dependencies in an entity controller. Since the entity controller already has a default controller receiving the DB context, when I create another constructor in my custom partial class with all the dependencies I need, it throws that runtime exception I mentioned.

The use case is: When a user creates an Order, I want the server to send an email to all users with a given role.
How can I achieve this?

You can try instead of constructor injection to get the services via HttpContext.RequestServices. Check here: Inject Services in Partial Controllers and here: [C#] Querying a table while creating another one

1 Like

Yes! Thank you very much korchev! I can't believe it's so simple....