extend ASP.NET Core Identity user

2020-05-07 06:51发布

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

2条回答
一纸荒年 Trace。
2楼-- · 2020-05-07 07:17

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.

查看更多
等我变得足够好
3楼-- · 2020-05-07 07:29

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.

查看更多
登录 后发表回答