0

Hi I have a problem accessing Users table from a Razor page in ASP.NET Core

I Created an AppDataContext class that extends IdentityDbContext<Models.User, Models.UserRole, string>

I can use it with other controller classes and services without problems. But when I start working on razor pages the dataContext.Users always return empty enumerable. Other DbSets still working properly, only the Users not work.

This also happens when I try to access data from other services like UserManager.Users or SigniInManager.UserManager.Users

Here's some part of my files

AppDataContext

public class AppDataContext : IdentityDbContext<Models.User, Models.UserRole, string>
{
    // Other DbSet's

    public AppDataContext(DbContextOptions<AppDataContext> options) : base(options) { }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        // Other entities building

        MapIdentityTables(builder);
    }

    private void MapIdentityTables(ModelBuilder builder)
    {
        builder.Entity<Models.User>().ToTable("Users");
        builder.Entity<IdentityUser>().ToTable("Users");
        builder.Entity<Models.UserRole>().ToTable("UserRoles");
        builder.Entity<IdentityRole>().ToTable("UserRoles");
        builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaims");
        builder.Entity<IdentityUserRole<string>>().ToTable("UserUserRoles");
        builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogins");
        builder.Entity<IdentityRoleClaim<string>>().ToTable("UserRoleClaims");
        builder.Entity<IdentityUserToken<string>>().ToTable("UserTokens");
    }

LoginPage.cshtml.cs

public class LoginModel : PageModel
{
    private readonly Identity.AppUserManager userManager;
    private readonly SignInManager<Models.User> signInManager;
    private readonly Data.AppDataContext dataContext;

    public LoginModel(Identity.AppUserManager userManager, SignInManager<Models.User> signInManager, Data.AppDataContext dataContext)
    {
        this.userManager = userManager;
        this.signInManager = signInManager;
        this.dataContext = dataContext;
    }

    public IList<Model.User> Users { get; private set; }

    public void OnGet()
    {
        Users = userManager.Users.ToList(); // Empty
        Users = signInManager.UserManager.Users.ToList(); // Empty
        Users = dataContext.Users.ToList(); // Empty
    }
}

User class

public class User : Microsoft.AspNetCore.Identity.IdentityUser
{
    public ICollection<UserDevice> Devices { get; set; }
    public IList<UserPassword> Passwords { get; set; }
}

Have I done anything wrong or am I missing something?

UPDATE

The problem is gone somehow now after I gave up and do something else. But it's not a solution since the original problem still there if I did the same.

What I have done was to revert all my changes and added AddSecondIdentity from this SO answer. Created StaffUser : IdentityUser and StaffUserManager<StaffUser, UserRole> (same UserRole as the original UserManager) to handle those new IdentityUser objects.

Then I just use StaffUserManager and SignInManager<StaffUser> instead of AppUserManager and SignInManager<User> in Login.cshtml.cs

public LoginModel(StaffUserManager userManager, SignInManager<Models.StaffUser> signInManager, Data.AppDataContext context)
{
    var users = context.Users.ToList() // 1 user
}

Which now confuses me further. But I don't have time for this now. I think it has something to do with the Discriminator part of the database since the returned user is the one with StaffUser discriminator value but there are some others without the discriminator that are not returned.

anuith
  • 659
  • 5
  • 16
  • May I see your User class. Do you want to use User class with IdentityUser ? If so please take a look on my code https://github.com/Awesome-CMS-Core/Awesome-CMS-Core/blob/master/src/AwesomeCMSCore/Modules/AwesomeCMSCore.Modules.Entities/Entities/User.cs –  Jun 25 '18 at 06:51
  • @NgôHùngPhúc I've added my `User` class in the original post. And yes, it derived from `IdentityUser` – anuith Jun 25 '18 at 09:49
  • Did you solve this issue –  Jun 28 '18 at 03:09
  • I kind of solved my issue as described in the UPDATE section. But the solution is not relevant with the original problem. So I don't think that I have solved the original problem yet. – anuith Jul 02 '18 at 10:44

1 Answers1

0

Make sure you add services.AddIdentity

 services
                    .AddIdentity<User, ApplicationRole>(options =>
                    {
                        options.Password.RequireDigit = false;
                        options.Password.RequiredLength = 4;
                        options.Password.RequireLowercase = false;
                        options.Password.RequireNonAlphanumeric = false;
                        options.Password.RequireUppercase = false;

                        //lock out attempt
                        options.Lockout.AllowedForNewUsers = true;
                        options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
                        options.Lockout.MaxFailedAccessAttempts = 3;
                    })
                    .AddEntityFrameworkStores<ApplicationDbContext>()
                    .AddDefaultTokenProviders();

If this doesn't work please provide me your source code so I can take a look on it. Perhaps your github.