Home > Enterprise >  Retrieve user information from ASPNetUsers table
Retrieve user information from ASPNetUsers table

Time:11-25

I am really new to using Blazor WASM and the ASP.NET Core hosted. I have set up the login which stores all registered users to the ASPNetUsers table. I am wondering how I can then retrieve these users to display information to other users. For example I am looking to be able to have a user logged in who can then search all other users who have registered into the application as well. How might I go about displaying a list of all the users who have registered onto the application, stored in ASPNetUsers table

Options

enter image description here

Retrieve and send back

    [HttpGet]
    public async Task<ActionResult<IEnumerable<User>>> Get()
    {
        var result = userManager.Users.FirstOrDefault();

        User x = new User();
        x.Username = result.Email;

        List<User> giveback = new List<User>();
        giveback.Add(x);
        
        return giveback;
    }

CodePudding user response:

To retrieve all users access the Users property on the UserManager:

var users = UserManager.Users.ToList();

The image in your post shows that you are only accessing the static members. You are not using an instance of UserManager.

You need to inject an instance into your controller:

readonly UserManager<ApplicationUser> _userManager;

public MyController(UserManager<ApplicationUser> userManager)
{
    _userManager = userManager;
}

[HttpGet]
public ActionResult<IEnumerable<Ticket>> Get()
{
   var user = _userManager.Users.FirstOrDefault();
}
  • Related