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