Wait for SignInManager.PasswordSignInAsync () always results in an error in my ASP.NET MVC project

I am currently implementing the ability to log into my ASP.NET MVC project. I am trying to use as many of the provided methods as possible.

I currently set up a table in my database known as User that stores all usernames, passwords, and other information about users.

My LoginViewModel:

public class LoginViewModel
{
    public User User { get; set; }
}

      

AccountController (which is basically the default, I just changed the variables of the PasswordSignInAsync method)

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        return View(model);
    }
    // To enable password failures to trigger account lockout, change to shouldLockout: true
    var result = await SignInManager.PasswordSignInAsync(model.User.Username, model.User.Password, false , shouldLockout: false);

      

Why does this line always return an error?

This is my first time trying to add the Login ability, so any help would be greatly appreciated. :)

+3


source to share


2 answers


Add to

app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

      



In method public void ConfigureAuth(IAppBuilder app)

inApp_Start\Startup.Auth.cs

+1


source


If the username is! = Email address:

I like:

comment out:



//var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

      

and add

ApplicationUser signedUser = UserManager.FindByEmail(model.Email);
var result = await SignInManager.PasswordSignInAsync(signedUser.UserName, model.Password, model.RememberMe, shouldLockout: false);

      

+16


source







All Articles