asp.net-corerazor-pagesasp.net-core-identity

How to extend the UserManager with new functionality in ASP.NET Core


I am working on ASP.NET Core 6.0. On the Registration.cshtml page, I have added a new field called UserCreatedUserName. The UserCreatedUserName data is not an email, it's just a unique string entered by the user.

I have created a new ApplicationUser derived from IdentityUser with this new field and is saved successfully in the AspNetUser table. Now, I want to get the user queried by this new field.

For example, the UserManager class has a method _userManager.FindByEmailAsync(email). I want to create a similar method to get the user by UserCreatedUserName, say _userManager.FindByUserCreatedUserNameAsync(UserCreatedUserName). I am new to identity and please let me know how to extend the UserManager class with this new method.


Solution

  • I tested the extension methods and also reproduced the error. After all, I found there's no problem with these methods. It is just because there's some default partial pages such as _LoginPartial.cshtml has some code still using IdentityUser.
    After changing this part, everything works.

    @inject SignInManager<IdentityUser> SignInManager   //Need change to ApplicationUser
    @inject UserManager<IdentityUser> UserManager      //Need change to ApplicationUser
    ...