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?
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.
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.
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.
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.
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>
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) { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With