3

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.

1 Answer 1

1

use CheckPasswordSignInAsync from SignInManager class to authenticate your user.

ApplicationUser user = await userManager.FindByEmailAsync(UxUsername.Text);
var signInResult = _signInManager.CheckPasswordSignInAsync(user, ,User.Password, false);

To use SignInManager you should inject it to your form class.

Note: SignInManager is in Microsoft.AspNetCore.Identity

Sign up to request clarification or add additional context in comments.

2 Comments

How do I bring in SignInManager to my Winforms app? I did look into that, but I couldn't get it to work in my project.
I was able to bring in SignInManager, but it I brought it in from Microsoft.AspNet.Identity.Owin - that allowed me to use _signInManager.PasswordSignIn, but that still gave me a SignInStatus failure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.