Search code examples
authenticationasp.net-core-mvcasp.net-identity

Identity Framework Get Application User after login


I use a custom class for automatic login the user with identity Framework then I use the ApplicationUser to get multiple information but now, ApplicationUser is null when login in is successfull, here's my code :

_signInManager.PasswordSignInAsync(username, pwd, RememberMe, lockoutOnFailure: false);
ApplicationUser = _userManager.GetUserAsync(_signInManager.Context.User).Result;

The code is not in a controller but in a custom class for login and user management, it work well yesterday but I cannot figure why it doesn't work now.

'_signInManager' and '_userManager' is created by dependency injection in the constructor.

EDIT
I've moved the code to a controller where I can access "User", the login is successfull but User.Identity.isAuthenticated is False


Solution

  • After you execute _signInManager.PasswordSignInAsync you need to have the cookie set on your request. This means you'll need to allow for a redirection to another page to happen before you can use Context.User.

    If you need ApplicationUser just after the sign-in just use _userManager.FindByNameAsync(username).