Changing IdentityUser Type in ASP.NET Core 2.1

asp.net-core asp.net-core-2.1 c# entity-framework-core

Question

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?

1
6
5/20/2018 3:20:29 AM

Accepted Answer

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>
14
5/19/2018 9:26:22 PM

Popular Answer

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) { }



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow