简体   繁体   中英

extend ASP.NET Core Identity user

Using structuremap for my DI. By default everything works, however I want to add fn and ln to AspNetUser DB. I've added a new class like so:

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

modified Startup.cs:

    services.AddDbContext<AppDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<AppDbContext>()
        .AddDefaultTokenProviders();

Modified ApplicationDbContext:

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

Controller (modified to AppliationUser from Identityuser):

    public AccountController(
        SignInManager<ApplicationUser> signInManager,
        UserManager<ApplicationUser> userManager)
    {
        m_signInManager = signInManager;
        m_userManager = userManager;
    }

Error i'm getting:

StructureMapConfigurationException: No default Instance is registered and cannot be automatically determined for type 'IUserStore'

There is no configuration specified for IUserStore

To Striter:

Is easy to miss an update to identity classes when creating subclases.

So, the idea is:

1) Check base clases you will override from identity: ApplicationUser, ApplicationRole, (if you modify it, not very common), ApplicationUserLogin, ApplicationClaims… etc. Note my example is taken from code I have where I modified UserRoles to include a company, so it's a bit more complex on the declaration.

For example, you may have:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{ // Code… }

Or (Not tested)

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, IdentityUserRole<int>, IdentityUserClaim<int>>
{ // Code… }

Then replace them accord in all subroutines. In case of ShaneKm, he probably left that unchanged on DbContext or UserStore:

public class ApplicationDbContext : IdentityDbContext<IdentityUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{ // Code … }

And fixed it like:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{ // Code … }

Fo search of missing replacements, right-click on "IdentityUser" in public class ApplicationUser : IdentityUser

2) Sometimes you compile and not all things are updated, so old objs still refers to already changed code. Happens to me frequently with WCF. You have to clean obj and bin directories (I delete them) to ensure everything gets recompiled.

Hope this clarifies.

Thanks to @Jorge Rojas I was able to solve it. Not all references were replaced with ApplicationUser. In my Container I did not have to make any changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM