Unable to get IdentityUser data from ASP.NET Core 2.1 controller

asp.net-core c# entity-framework-core identity

Question

I am developing an application in ASP.NET Core 2.1. I am using Identity with built-in user accounts. Now I am trying to develop a user maintenance module in this application where I'm stuck. I am unable to fetch the data inside the controller. Here is my code:

ApplicationDbContext.cs:

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public DbSet<ApplicationUser> ApplicationUsers { get; set; }

}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddIdentity<IdentityUser,IdentityRole>(config => {
            config.SignIn.RequireConfirmedEmail = true;
        })
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultUI()
        .AddDefaultTokenProviders();

        services.AddMemoryCache();
        services.AddSession();

        services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddSessionStateTempDataProvider();

        services.AddSingleton<IEmailSender, EmailSender>();
    }

ApplicationUser.cs:

public class ApplicationUser : IdentityUser
{
    [Required]
    public string FirstName { get; set; }

    [Required]
    public string Name { get; set; }
}

Index.cshtml:

@model IEnumerable<TestIdentity.Models.ApplicationUser>
@{
    ViewData["Title"] = "Index";
}
<h2>Application users</h2>
@foreach (var item in Model)
{
    <p>@item.Name</p>
}

ApplicationUsersController.cs::

public class ApplicationUsersController : Controller
{
    private readonly ApplicationDbContext _context;

    public ApplicationUsersController(ApplicationDbContext context)
    {
        _context = context;
    }

    // GET: Administrator/ApplicationUsers
    public async Task<IActionResult> Index()
    {
        return View(await _context.ApplicationUsers.ToListAsync());
    }
}

_context.ApplicationUsers.ToListAsync() is not returning any results, although there is data in my AspNetUsers table.

1
1
12/17/2018 11:16:03 PM

Accepted Answer

If you would like to use custom ApplicationUser for your Identity instead of default IdentityUser.You need to make some changes after declaring the model.

1.In dbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

2.In startup.cs:

services.AddIdentity<ApplicationUser,IdentityRole>(config => {
        config.SignIn.RequireConfirmedEmail = true;
    })
...

3.In your /Shared/_LoginPartial.cshtml

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
5
12/18/2018 2:49:34 AM

Popular Answer

IdentityUser is not a part of your dbContext. You need to use UserManager<IdentityUser> instead.

Your ApplicationUsersController class should look like this

public class ApplicationUsersController : Controller
{
    private readonly ApplicationDbContext _context;
    private readonly UserManager<IdentityUser> _userManager;

    public ApplicationUsersController(ApplicationDbContext context,
                                      UserManager<IdentityUser> userManager;)
    {
        _context = context;
        _userManager = userManager;
    }

    // GET: Administrator/ApplicationUsers
    public async Task<IActionResult> Index()
    {
        return View(await _userManager.Users.Cast<ApplicationUser>().ToListAsync());
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow