0

I'm seeing odd behavior with the login/logout routes. I have the front-end set up using React. When the Login button is clicked, it makes an ajax call to a route /login, which then runs the login function below:

[HttpPost]
public async Task<IActionResult> Post([FromBody] LoginModel loginModel) {
  if (!ModelState.IsValid) {
    return new BadRequestResult();
  }
  var signInResult = await SignInManager.PasswordSignInAsync(loginModel.Username, loginModel.Password, false, false);
  if (signInResult.Succeeded) {
    var currentUser = await UserManager.GetUserAsync(User);
    if (currentUser == null) {
      return new BadRequestResult();
    }
    var myUserModel = Mapper.Map<MyUser, MyUserModel>(currentUser);
    return new OkObjectResult(myUserModel);
  }
  return new BadRequestResult();
}

The issue is that I have to click the button twice. The first time I click it, this function hits currentUser == null, so it returns a 400. But the second time I click it, it works just fine.

I am trying to login, then return the user object back to the front-end.

JB1
  • 21
  • 3
  • Possible duplicate of [\_userManager.GetUserAsync(User) returns null](https://stackoverflow.com/questions/46136037/usermanager-getuserasyncuser-returns-null) – Sonal Borkar Dec 18 '18 at 22:40
  • 2
    can't use var currentUser = await UserManager.GetUserAsync(User); until the cookie is set and redirected from browser just after signin. Try get user by loginModel.Username – Sonal Borkar Dec 18 '18 at 22:42
  • Oh! Yep that was it. Thanks @SonalBorkar! – JB1 Dec 18 '18 at 23:02

0 Answers0