public ActionResult Register(Patient patient, HttpPostedFileBase uploaded) { if (ModelState.IsValid) { var isExist = SecurityUtilities.IsEmailExists(patient.Email); if (isExist) { //username is registered before ModelState.AddModelError("EmailExist", "Email already exists ."); ViewBag.BloodGroupID = new SelectList(db.BloodGroups, "ID", "Name", patient.BloodGroupID); return(View(patient)); } //Completing user model data patient.PW = SecurityUtilities.Hash(patient.PW); if (uploaded != null && uploaded.ContentLength > 0) { string extension = Path.GetExtension(uploaded.FileName); string pattern = @".(jpg|JPG|jpeg|JPEG|png|PNG)$"; if (Regex.IsMatch(extension, pattern)) { // convert image to array of binary patient.Img = new byte[uploaded.ContentLength]; uploaded.InputStream.Read(patient.Img, 0, uploaded.ContentLength); } else { ModelState.AddModelError("ImgError", "Only Images allowed ."); ViewBag.BloodGroupID = new SelectList(db.BloodGroups, "ID", "Name", patient.BloodGroupID); return(View(patient)); } } // insert user data in User and UserRoles tables in one transaction db.Patients.Add(patient); db.SaveChanges(); //Registeration succeeded, Sign in this account Response.Cookies.Add(SecurityUtilities.CreateAuthenticationCookie(patient.FName, patient.ID.ToString())); // Redirect to Dashboard return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("RegisterError", "An error occured while registeration ."); } // If we got this far, something failed, redisplay form ViewBag.BloodGroupID = new SelectList(db.BloodGroups, "ID", "Name", patient.BloodGroupID); return(View(patient)); }
public UserRepasswdResult User_repasswd(long userId, string oldPasswd, string newPasswd) { using (var gpEntities = new GPEntities()) { var hashedPassword = GetHashedPassword(userId); if (hashedPassword == null) { return(UserRepasswdResult.Wrong); } if (!Crypto.VerifyHashedPassword(hashedPassword, oldPasswd)) { return(UserRepasswdResult.Wrong); } var hashedNewPasswd = Crypto.HashPassword(newPasswd); var q = gpEntities.users.FirstOrDefault(u => u.user_id == userId); if (q == null) { return(UserRepasswdResult.Wrong); } q.passwd = hashedNewPasswd; gpEntities.SaveChanges(); return(UserRepasswdResult.Ok); } }