C
C#2y ago
Gipper

How to use UserManager in my controller?

What the title says, what I'm doing currently:
private SignInManager<IdentityUser> _signInManager;
private UserManager<IdentityUser> _userManager;

public UtilizadoresController(ApplicationDbContext context, SignInManager<IdentityUser> signInManager, UserManager<IdentityUser> userManager)
{
_context = context;
_signInManager = signInManager;
_userManager = userManager;
}
private SignInManager<IdentityUser> _signInManager;
private UserManager<IdentityUser> _userManager;

public UtilizadoresController(ApplicationDbContext context, SignInManager<IdentityUser> signInManager, UserManager<IdentityUser> userManager)
{
_context = context;
_signInManager = signInManager;
_userManager = userManager;
}
But Visual Studio doesn't recognize the UserManager<IdentityUser> type: 'UserManager<>' is an ambiguous reference between 'Microsoft.AspNet.Identity.UserManager<TUser>' and 'Microsoft.AspNetCore.Identity.UserManager<TUser>' When I change it to private Microsoft.AspNet.Identity.UserManager<IdentityUser> _userManager; I get this error: 'Microsoft.AspNetCore.Identity.IdentityUser' cannot be used as type parameter 'TUser' in the generic type or method 'UserManager<TUser>'. There is no implicit reference conversion from 'Microsoft.AspNetCore.Identity.IdentityUser' to 'Microsoft.AspNet.Identity.IUser<string>'.
18 Replies
Angius
Angius2y ago
Try IdentityUser<string> maybe?
Gipper
GipperOP2y ago
As in private IdentityUser<string> _userManager; ?
Angius
Angius2y ago
Yeah And in the constructor
Gipper
GipperOP2y ago
it builds, but then when I go to the view I get:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IdentityUser`1[System.String]' while attempting to activate 'Trabalho_Lab_Aplicações_Web.Controllers.UtilizadoresController'.
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IdentityUser`1[System.String]' while attempting to activate 'Trabalho_Lab_Aplicações_Web.Controllers.UtilizadoresController'.
Angius
Angius2y ago
Huh Not sure, then I wonder if you need to create your own user type...? Probably not Ah, wait UserManager<IdentityUser<string>> not just IdentityUser<string>
Gipper
GipperOP2y ago
Get the same ambiguous reference error from before. Could it be soething wrong in the Program.cs?
Angius
Angius2y ago
Not sure tbh
Gipper
GipperOP2y ago
I removed the ambiguity by changing it to Microsoft.AspNetCore.Identity.UserManager<IdentityUser<string>> and now it seems to be working, at least the instance of that objet recognizes a changepasswordasync method
Angius
Angius2y ago
Ah, nice
Gipper
GipperOP2y ago
Still get the invalid exception tho on the view
Angius
Angius2y ago
What invalid exception?
MODiX
MODiX2y ago
Gipper
it builds, but then when I go to the view I get:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IdentityUser`1[System.String]' while attempting to activate 'Trabalho_Lab_Aplicações_Web.Controllers.UtilizadoresController'.
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IdentityUser`1[System.String]' while attempting to activate 'Trabalho_Lab_Aplicações_Web.Controllers.UtilizadoresController'.
Quoted by
React with ❌ to remove this embed.
Angius
Angius2y ago
Huh At this point, I'd just create a new Razor Pages project with scaffolded Identity and see how they do it
Gipper
GipperOP2y ago
Fixed it, I think. It was the program.cs where for some reason I had a call to AddDefaultIdentity instead of just AddIdentity
Angius
Angius2y ago
Ah
Gipper
GipperOP2y ago
The controller at least constructs fine now even with usermanager and signinmanager
Angius
Angius2y ago
Nice

Did you find this page helpful?