0

When using GeneratePasswordResetTokenAsync I get three invalid column warnings, two for UserId and one for RoleId. I believe this has something to do with AspNetUsers relationships, which I have not edited in my database. I am running the code below. I've edited the code below to show where dbUser is derived from, which gets its context from the web.config's DefaultConnection.

using (var appContext = ApplicationDbContext.Create())
{
    var dbUser = appContext.Users.FirstOrDefault(x => x.UserName == Username);                            
    if (dbUser.EmailConfirmed)
    {
        var provider = Startup.DataProtectionProvider;
        var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(appContext));
        userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(provider.Create("PasswordReset"));
        token = await userManager.GeneratePasswordResetTokenAsync(dbUser.Id);

        ...
    }
}

I tried adding an extended context with this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AspNetUser>()
            .HasMany(n => n.AspNetUserClaims)
            .WithRequired() 
            .Map(a => a.MapKey("UserId"));

        modelBuilder.Entity<AspNetUser>()
            .HasMany(n => n.AspNetUserLogins)
            .WithRequired() 
            .Map(a => a.MapKey("UserId"));

        modelBuilder.Entity<AspNetUser>()
            .HasMany(n => n.AspNetRoles)
            .WithRequired()
            .Map(a => a.MapKey("RoleId"));
    }

But this had no effect. What I don't understand is why the call to GeneratePasswordResetTokenAsync would not understand the relationship to it's connected tables, they are all AspNet_X tables. In other words they are AspNetRoles (through AspNetUserRoles), AspNetUserClaims, and AspNetUserLogins.

Am I just looking at this issue wrong?

Hammer
  • 1
  • 2
  • If you're using the Identity framework, building the relations between the tables, is not needed, if not messing things up. Can you provide how you get the dbUser? Make sure it shares the same appContext instance with userManager. – Kostas Dafnomilis Jul 20 '20 at 08:58
  • Hi @KostasDafnomilis, yes the dbUser is using the same ApplicationDBContext as the userManager. I didn't build any relations between tables, these are the ones from the database. – Hammer Jul 20 '20 at 21:53
  • There is nothing odd to the code that you have provided. To help find a solution, more code samples are needed. To begin with, could you please edit your question to add how you retrieve dbUser from the database? – Kostas Dafnomilis Jul 21 '20 at 09:21
  • @KostasDafnomilis, I added the code you asked for. I agree there is nothing odd about the code. The question is, why is the ApplicationUserManager expecting these non-existing fields. My understanding is the EntityFramework can display this issue when a class is derived from a parent, but that is not the case here. The tables are the normal identity tables in SQL Server because that context provider is a System.Data.SqlClient. – Hammer Jul 21 '20 at 12:42

0 Answers0