C
C#5mo 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
Angius5mo ago
Try IdentityUser<string> maybe?
Gipper
Gipper5mo ago
As in private IdentityUser<string> _userManager; ?
Angius
Angius5mo ago
Yeah And in the constructor
Gipper
Gipper5mo 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
Angius5mo 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
Gipper5mo ago
Get the same ambiguous reference error from before. Could it be soething wrong in the Program.cs?
Angius
Angius5mo ago
Not sure tbh
Gipper
Gipper5mo 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
Angius5mo ago
Ah, nice
Gipper
Gipper5mo ago
Still get the invalid exception tho on the view
Angius
Angius5mo ago
What invalid exception?
MODiX
MODiX5mo 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
Angius5mo ago
Huh At this point, I'd just create a new Razor Pages project with scaffolded Identity and see how they do it
Gipper
Gipper5mo 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
Angius5mo ago
Ah
Gipper
Gipper5mo ago
The controller at least constructs fine now even with usermanager and signinmanager
Angius
Angius5mo ago
Nice