Search code examples
c#asp.netasp.net-identity

Asp.Net Identity - Custom Database Access UserManager


Is is possible to extend the UserStore/UserManager to have access to a custom table within an override method (without creating another dbcontext)?

I'm working on a legacy database which we integrated with Identity. Recently we added a new table, Clients, to the database that has a one to one relationship with the Users table. What would like to do is override the FindAsync method in the UserManager class to do call the base method, grab the user, and then based on the user's client Id stored in the table, fetch the client name and insert that into the user model.

The following is what I have attempted; however, to me I think this is wrong approach and would like some suggestions on how to correctly apply what I'm trying to do.

public override async Task<ApplicationUser> FindAsync(string userName, string password)
{
    ApplicationUser user = await base.FindAsync(userName, password);

    if (user != null && user.ClientID != null)
    {
        using (var ctx = new ApplicationDbContext(HostPage.ConnectionString))
        {
            user.Client = ctx.Clients.Where(p => p.ClientID == user.ClientID).FirstOrDefault();
        }
    }

    return user;
}

Solution

  • You could modify SQL in base.FindAsync() to perform a join to your table thus executing a single query.