I have a random error in login action. It is an application with MVC 5.2 and AspNet.Identity 2.
When some user try login SignInStatus.Failure
error occurs in:
SignInStatus result = await
SignInManager.PasswordSignInAsync(
model.Email,
model.Password,
model.RememberMe, shouldLockout: false);
This happens randomly with users, if a user connects fine, then it may fail to connect.
Registration done correctly, then this may or may not connect. is not asking for confirmation of the account.
The first time I probe the system worked correctly, the next day I started this local problem or on the server.
SignInManager.PasswordSignInAsync requires Username and Password not an Email and Password so if you have Username same as Email it will work with you else it won't work.
here is the signature of the PasswordSignInAsync MSDN
public virtual Task<SignInStatus> PasswordSignInAsync(
string userName,
string password,
bool isPersistent,
bool shouldLockout
)
to allow SignIn using Email while having different Username, modify you AccountController.Login to the following
User user = null;
using (DbContext db = new DbContext())
{
user = db.Users.Single(u => u.Email.Equals(model.Email));
}
if (user != null)
{
var result = SignInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new {ReturnUrl = returnUrl, RememberMe = model.RememberMe});
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
else
{
//Handle User not found
}