I am using C# in a Winforms app and I have Identity wired up to where I can find users in the database. I have a simple login form that I cannot get working. I can find the user but I cannot verify the user via the password. I have a textbox Username, and one for Password, a button, and a Label for displaying the result. Here is my code:
private async void LoginButton_ClickAsync(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(Username.Text) &&
!String.IsNullOrEmpty(Password.Text))
{
var context = new MyDbContext();
var userManager = new UserManager<ApplicationUser>(new
UserStore<ApplicationUser>(context));
//THIS SUCCESSFULLY FINDS THE USER
ApplicationUser user = await userManager.FindByEmailAsync(UxUsername.Text);
if (user == null)
{
ResultLabel.Text = "NOT FOUND";
}
if (userManager.PasswordHasher.VerifyHashedPassword(user.PasswordHash, Password.Text)
!= PasswordVerificationResult.Failed)
{
ResultLabel.Text = "MATCH FOR USER AND PASSWORD!";
}
ResultLabel.Text = "NOT FOUND";
}
}
I have also tried this and I could not even get the user back - it was null:
ApplicationUser user = await userManager.FindAsync(Username.Text, Password.Text);
I am using this same database for an MVC app and I am logging in just fine with the same exact credentials.
Thanks.