public ActionResult Register(RegisterModel model, string m_txt_ho_ten) { if (ModelState.IsValid) { // Attempt to register the user try { QLTC_MVCEntities db = new QLTC_MVCEntities(); USER user = new USER(); if (m_txt_ho_ten != null) { user.USER_NAME = m_txt_ho_ten; } else { user.USER_NAME = model.UserName; } user.ACCOUNT_NAME = model.UserName; user.PASSWORD = model.Password; db.USERs.Add(user); db.SaveChanges(); WebSecurity.CreateUserAndAccount(model.UserName, model.Password); WebSecurity.Login(model.UserName, model.Password); return(RedirectToAction("Index", "Home")); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form return(View(model)); }
public decimal get_id_user() { QLTC_MVCEntities db = new QLTC_MVCEntities(); lst_user = db.USERs.Where(x => x.ACCOUNT_NAME == System.Web.HttpContext.Current.User.Identity.Name).ToList(); if (lst_user.Count > 0) { return(lst_user[0].ID); } return(-1); }
private decimal get_id_user(string ip_user_name) { QLTC_MVCEntities db = new QLTC_MVCEntities(); lst_user = db.USERs.Where(x => x.ACCOUNT_NAME == ip_user_name).ToList(); if (lst_user.Count > 0) { return(lst_user[0].ID); } return(-1); }
public ActionResult Manage(LocalPasswordModel model) { bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name)); ViewBag.HasLocalPassword = hasLocalAccount; ViewBag.ReturnUrl = Url.Action("Manage"); if (hasLocalAccount) { if (ModelState.IsValid) { // ChangePassword will throw an exception rather than return false in certain failure scenarios. QLTC_MVCEntities db = new QLTC_MVCEntities(); decimal id_user = get_id_user(User.Identity.Name); bool changePasswordSucceeded; if (id_user == -1) { changePasswordSucceeded = false; } try { changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword); //thay doi mat khau trong database USER user = db.USERs.Find(id_user); user.PASSWORD = model.NewPassword; db.SaveChanges(); } catch (Exception) { changePasswordSucceeded = false; } if (changePasswordSucceeded) { return(RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess })); } else { ModelState.AddModelError("", "Mật khẩu hiện tại là không chính xác hoặc mật khẩu mới là không hợp lệ"); } } } else { // User does not have a local password so remove any validation errors caused by a missing // OldPassword field ModelState state = ModelState["OldPassword"]; if (state != null) { state.Errors.Clear(); } if (ModelState.IsValid) { try { WebSecurity.CreateAccount(User.Identity.Name, model.NewPassword); return(RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess })); } catch (Exception e) { ModelState.AddModelError("", e); } } } // If we got this far, something failed, redisplay form return(View(model)); }