Zeus82
Zeus82

Reputation: 6375

Changing IdentityUser Type in ASP.NET Core 2.1

I want my User to have a Guid as the primary key, but when I create my own user type, my website throws an exception on startup.

Anyone knows how to change IdentityUser type?

I did this:

services.AddIdentity<MyUser, MyRole>()
    .AddEntityFrameworkStores<UBContext>()
    .AddDefaultUI()
    .AddDefaultTokenProviders();

But when my program starts up I get this error:

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.

Does that have something to do with the fact that the identity UI is now in a separate lib and a controller in that lib is expecting a UserManager<IdentityUser>?

How can I override that?

Upvotes: 6

Views: 5163

Answers (2)

Zeus82
Zeus82

Reputation: 6375

Ok, so found the problem. _LoginPartial.cshtml injects

@inject SignInManager<IdentityUser>
@inject UserManager<IdentityUser>

so you need to make sure that they are updated to

@inject SignInManager<MyUser>
@inject UserManager<MyUser>

Upvotes: 16

Mike Bovenlander
Mike Bovenlander

Reputation: 5416

You should extend the IdentityUser from your MyUser class:

public class MyUser : IdentityUser
{
    public string FirstName { get; set; }

    public string LastName { get; set; }
}

Then registering the new user class:

services.AddIdentity<MyUser , MyRole>()

And finally, where your UserManager is injected, add your own type:

public HomeController(UserManager<MyUser> userManager) { }

Upvotes: 1

Related Questions