Saturday, August 12, 2017

Cannot login on ASP.NET Identity 2 site after programmatic user creation. ApplicationUser userAdmin = new ApplicationUser { Id = "admin", Email = "me@there.com", UserName = "admin" };

I faced this stressing issue. It has to be with the default Login method created in the AccountsController:





You can see that
"await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);"
is including the Email as the first parameter, but if you check the definition for PasswordSignInAsync you will see that it MUST receive the Username as the first parameter and not the Email.
It works by default because the method for Registering a New User sets both properties (Username and Email) equals to the user-provided email value. But if you saved a new user programmatically and set Username and Email to different values, Login will fail.
So you know now what to do: 1)Change your login method to pass username as the first argument if change Register method to save username and email with different values. I hope it helps someone else.

No comments:

Post a Comment