How to List Users with Asp.Net MVC 5

Asked

Viewed 2,001 times

5

I am using Visual Studio 2013, I am creating a new Asp.NET Web Application project using MVC and authentication by "Individual User Account".

I would like to know how to list all registered users.

Models:

Identitymodel

using Microsoft.AspNet.Identity.EntityFramework;

namespace WebApplication9.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }
    }
}

Accountviewmodels

using System.ComponentModel.DataAnnotations;

namespace WebApplication9.Models
{
    public class ExternalLoginConfirmationViewModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }
    }

    public class ManageUserViewModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "New password")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm new password")]
        [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

    public class LoginViewModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }

    public class RegisterViewModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }
}
  • 1

    You can display any list with Entityframework?

  • Yes, I can do it.

1 answer

3


If context is mapped correctly, the following Controller can solve:

public class UsuariosController : Controller
{
    public UserManager<ApplicationUser> UserManager { get; private set; }
    public RoleManager<IdentityRole> RoleManager { get; private set; }
    public MyDbContext context { get; private set; }

    public UsuariosController()
    {
        context = new MyDbContext();
        UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
        RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
    }

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

    //
    // GET: /Users/
    public async Task<ActionResult> Index()
    {
        return View(await UserManager.Users.ToListAsync());
    }
}

The context should have the following configuration:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<IdentityUser>()
        .ToTable("Users");
    modelBuilder.Entity<ApplicationUser>()
        .ToTable("Users");
}

Make sure your solution already uses these packages (packages.config). If yes, try to close the Solution and open again.

  <package id="Microsoft.AspNet.Identity.Core" version="2.0.0-alpha1" targetFramework="net45" />
  <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.0.0-alpha1" targetFramework="net45" />
  <package id="Microsoft.AspNet.Identity.Owin" version="2.0.0-alpha1" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Owin" version="2.0.2" targetFramework="net45" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="2.0.2" targetFramework="net45" />
  <package id="Microsoft.Owin.Security" version="2.0.2" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Cookies" version="2.0.2" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Facebook" version="2.0.2" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Google" version="2.0.2" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.MicrosoftAccount" version="2.0.2" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.OAuth" version="2.0.2" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Twitter" version="2.0.2" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Owin" version="1.0" targetFramework="net45" />

Full example:

https://github.com/rustd/AspnetIdentitySample

  • Gypsy, look at the error returned: 'Microsoft.AspNet.Identity.Usermanager<Webapplication9.Models.Applicationuser>' does not contain a Definition for 'Users' and no Extension method 'Users' Accepting a first argument of type.

  • The only change I made was in context, mine is Applicationdbcontext.

  • 1

    Your ApplicationUser drift IdentityUser? You can post your code Models in the question?

  • The Applicationuser derives from Identityuser. I put the code there.

  • I updated the answer. There may be some configuration missing in your context.

  • The error continues: '... UserManager<WebApplication9.Models.ApplicationUser>' does not contain a definition for 'Users' and no extension method 'Users' accepting a first argument of type 'Microsoft.AspNet.Identity.UserManager<WebApplication9.Models.ApplicationUser>' could be found (are you Missing a using Directive or an Assembly Reference?) C: Users Renatto.Machado.Desrenatto Documents visual studio 2013 Projects Webapplication9 Webapplication9 Controllers Usuarioscontroller.Cs 36 43 Webapplication9

  • There must be something wrong with the configuration of your project. Here I replicated the example and it worked. I made another edit.

  • Gypsy, what I did was this: New Project > ASP.NET Web Application > I chose MVC Template > Changed Authentication to Individual User Account > OK button. So I think it’s not something wrong with the project setup.

  • Some suggestion Gypsy?

  • I did the same and here everything worked after updating packages. Try pressing <kbd>F12</kbd> on UserManager to check that the reference is correct.

  • I did what you said, I was taken to Assembly Microsoft.AspNet.Identity.Core.dll, v2.0.0.0, where the Usermanager class is set.

  • And it doesn’t have a Property called Users within that UserManager?

  • Not really. That’s all you get: public class UserManager<TUser> : UserManager<TUser, string> where TUser : class, Microsoft.AspNet.Identity.IUser<string>&#xA; { &#xA; public UserManager(IUserStore<TUser> store);&#xA; }

  • The package is outdated. Try updating.

  • Gypsy, I’ve entered the Tools > Extensions and Update > Update menu. All packages are up to date. How do I update what you said, in this case?

  • View > Other Windows > Package Manager Console. Enter the command Update-Package Microsoft.AspNet.Identity.Core.

  • 1

    Problem solved!!! Thanks Gypsy!

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.