public ActionResult Index(LoginViewModel model, string returnUrl = "") { if (ModelState.IsValid) { var customMembershipProvider = new CustomMembershipProvider(); if (customMembershipProvider.Authenticate(model.Username, model.Password)) { return RedirectToAction("Index", "Home"); } ModelState.AddModelError("", "Incorrect username and/or password"); } return View(model); }
public ActionResult Index(LoginViewModel model, string returnUrl = "") { if (ModelState.IsValid) { var user = Context.Users.Where(u => u.Username == model.Username && u.Password == model.Password).FirstOrDefault(); if (user != null) { var roles=user.Roles.Select(m => m.RoleName).ToArray(); CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel(); serializeModel.UserId = user.UserId; serializeModel.FirstName = user.FirstName; serializeModel.LastName = user.LastName; serializeModel.roles = roles; string userData = JsonConvert.SerializeObject(serializeModel); FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, user.Email, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData); string encTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); Response.Cookies.Add(faCookie); if(roles.Contains("Admin")) { return RedirectToAction("Index", "Admin"); } else if (roles.Contains("User")) { return RedirectToAction("Index", "User"); } else { return RedirectToAction("Index", "Home"); } } ModelState.AddModelError("", "Incorrect username and/or password"); } return View(model); }
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 ActionResult Login(LoginViewModel model, string returnUrl = "") { if (ModelState.IsValid) { var user = _db.Users.Where(u => u.Email == model.Email && u.Password == model.Password && u.IsActive == true).FirstOrDefault(); if (user != null) { var roles = user.Roles.Select(m => m.RoleName).ToArray(); CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel(); serializeModel.UserId = user.UserId; serializeModel.FirstName = user.FirstName; serializeModel.LastName = user.LastName; serializeModel.Email = user.Email; serializeModel.roles = roles; string userData = JsonConvert.SerializeObject(serializeModel); FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, user.Email, DateTime.Now, DateTime.Now.AddDays(1), false, userData); string encTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); Response.Cookies.Add(faCookie); if (!String.IsNullOrEmpty(returnUrl)) { return Redirect(returnUrl); } if (roles.Contains("Admin")) { return RedirectToAction("Index", "AdminInformationHospital"); } else if (roles.Contains("HospitalAdmin")) { return RedirectToAction("Index", "HospitalAdmin"); } else if (roles.Contains("Doctor")) { return RedirectToAction("Index", "Doctor"); } else if (roles.Contains("User")) { return RedirectToAction("Index", "UserInfo"); } else { return RedirectToAction("Index", "Home"); } } ModelState.AddModelError("", "Email hoặc mật khẩu không đúng."); } return View(model); }