Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 681
Zeus82 Avatar asked May 19 '18 18:05

Zeus82


People also ask

What is identity in ASP NET Core?

By Arthur Vickers ASP.NET Core Identity provides a framework for managing and storing user accounts in ASP.NET Core apps. Identity is added to your project when Individual User Accounts is selected as the authentication mechanism. By default, Identity makes use of an Entity Framework (EF) Core data model.

How do I change authentication in ASP NET Core?

1 Select File > New > Project. 2 Select ASP.NET Core Web Application. Name the project WebApp1 to have the same namespace as the project download. Click OK. 3 Select an ASP.NET Core Web Application, then select Change Authentication. 4 Select Individual User Accounts and click OK.

What is ASP NET Core adddefaultidentity?

AddDefaultIdentity was introduced in ASP.NET Core 2.1. Calling AddDefaultIdentity is similar to calling the following: See AddDefaultIdentity source for more information. See this GitHub issue for information on configuring Identity using SQLite. ASP.NET Core Identity: Is an API that supports user interface (UI) login functionality.

How do I customize a model in ASP NET Core?

Customize the model The starting point for model customization is to derive from the appropriate context type. See the Model generic types section. This context type is customarily called ApplicationDbContext and is created by the ASP.NET Core templates.


2 Answers

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>
like image 177
Zeus82 Avatar answered Sep 19 '22 12:09

Zeus82


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

like image 28
Mike Bovenlander Avatar answered Sep 20 '22 12:09

Mike Bovenlander