public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return View(model); } // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, change to shouldLockout: true var result = await SignInManager.PasswordSignInAsync(model.Email, 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); } }
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { var user = UserManager.FindByName(model.Email); if (user == null) { ModelState.AddModelError("", "Invalid login attempt.."); if (Request.IsAjaxRequest()) { return PartialView("~/Views/Mondofi/_SignInModal.cshtml", model); } return View(model); } if (user.Approved == false) { ModelState.AddModelError("", "Your Account is not Approved"); if (Request.IsAjaxRequest()) { return PartialView("~/Views/Mondofi/_SignInModal.cshtml", model); } return View(model); } string databasename = user.RestaurantName; databasename = databasename.Replace(" ", ""); UsersContext db = new UsersContext(databasename); using (var companyUserManger = ApplicationUserManager.Create(databasename)) using (var companySignInManager = ApplicationSignInManager.Create(databasename)) { var getUser = db.Users.Where(c => c.RestaurantName == user.RestaurantName).ToList(); if (getUser.Count() == 0) { var getresult = companyUserManger.Create(new UserProfile { Email = user.Email, FirstName = user.FirstName, LastName = user.LastName, UserName = user.Email, UserCode = 1234, PhotoPath = user.PhotoPath, Availability = user.Availability, DesignationId = user.DesignationId, RestaurantName = databasename, TermAndCondition = true, Approved = false, EmailConfirmed = true }, model.Password); if (getresult.Succeeded) { var getUseradd = db.Users.Where(c => c.Email == model.Email).FirstOrDefault(); companyUserManger.AddToRole(getUseradd.Id, "SuperAdmin"); } } var companyUser = companyUserManger.FindByName(model.Email); if (companyUser == null) { ModelState.AddModelError("", "Invalid login attempt."); if (Request.IsAjaxRequest()) { return PartialView("~/Views/Mondofi/_SignInModal.cshtml", model); } return View(model); } //Add this to check if the email was confirmed. if (!await companyUserManger.IsEmailConfirmedAsync(companyUser.Id)) { ModelState.AddModelError("", "You need to confirm your email."); if (Request.IsAjaxRequest()) { return PartialView("~/Views/Mondofi/_SignInModal.cshtml", model); } return View(model); } var result = await companySignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); switch (result) { case SignInStatus.Success: if (Request.IsAjaxRequest()) { return Json("Success", JsonRequestBehavior.AllowGet); } return RedirectToAction("FloorPlan", "FloorPlan"); 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"); if (Request.IsAjaxRequest()) { return PartialView("~/Views/Mondofi/_SignInModal.cshtml", model); } return View(model); } } } return View(model); }