public ActionResult ForgotPassword(ManageUserViewModel model) { if (ModelState.IsValid) { ServiceLayer.Services.ResetPasswordService _ResetPasswordService = new ServiceLayer.Services.ResetPasswordService(); var token = ""; string UserName = WebSecurity.CurrentUserName; //check user existance var user = Membership.GetUser(UserName); bool changePasswordSucceeded; changePasswordSucceeded = user.ChangePassword(model.OldPassword, model.NewPassword); if (!changePasswordSucceeded) { return(Content("Current password is not correct.")); } if (user == null) { TempData["Message"] = "User Not exist."; } else { //generate password token token = WebSecurity.GeneratePasswordResetToken(UserName); //create url with above token } bool any = _ResetPasswordService.UpdatePassword(UserName, token); bool response = false; if (any == true) { response = WebSecurity.ResetPassword(token, model.NewPassword); if (response == true) { try { // Here Maintain Password History // MembershipUser u = Membership.GetUser(WebSecurity.CurrentUserName, false); string RetPassword = HashData(model.NewPassword); SecUserPasswordHistory _secUserPasswordHistory = new SecUserPasswordHistory(); byte[] array = Encoding.ASCII.GetBytes(RetPassword); _secUserPasswordHistory.PasswordHash256 = array; _secUserPasswordHistory.DeleteFlag = false; _secUserPasswordHistory.RowVersion = null; _secUserPasswordHistory.SecUserID = (WebSecurity.CurrentUserId); _ResetPasswordService.AddPasswordHistory(_secUserPasswordHistory); TempData["Message"] = "Password changed."; } catch (Exception ex) { TempData["Message"] = "Error occured while changing Password." + ex.Message; } } else { TempData["Message"] = "Hey, avoid random request on this page."; } } else { TempData["Message"] = "Username and token not maching."; } } return(View(model)); }
public ActionResult Register(RegisterViewModel model) { //Validating Captcha string stringResponse = string.Empty; if (!ValidateCaptcha(out stringResponse)) { ModelState.AddModelError("", stringResponse); //Below code regarding Invalid captcha has been commented as currently we dont have any secret key for this application //return View(model); } ServiceLayer.Services.ResetPasswordService _ResetPasswordService = new ServiceLayer.Services.ResetPasswordService(); //if (ModelState.IsValid) { #region Commented Code //var user = new ApplicationUser() { UserName = model.UserName }; //var result = await UserManager.CreateAsync(user, model.Password); //if (result.Succeeded) //{ // await SignInAsync(user, isPersistent: false); // return RedirectToAction("Index", "Home"); //} //else //{ // AddErrors(result); //} #endregion try { List <User> list = _ResetPasswordService.GetUsersByEmail(model.UserName.ToString()); int _userID = WebSecurity.GetUserId(model.UserName); if (list.Count == 0 && _userID > 0) { ((SimpleMembershipProvider)Membership.Provider).DeleteUser(model.UserName.ToString(), true); // deletes record from webpages_Membership table } WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Active = false }); //TODO This Code Use For Mainain Password History string RetPassword = HashData(model.Password); SecUserPasswordHistory _secUserPasswordHistory = new SecUserPasswordHistory(); byte[] array = Encoding.ASCII.GetBytes(RetPassword); userService.UpdateUserInfo(model.UserName); _secUserPasswordHistory.PasswordHash256 = array; _secUserPasswordHistory.DeleteFlag = false; _secUserPasswordHistory.RowVersion = null; _secUserPasswordHistory.SecUserID = WebSecurity.GetUserId(model.UserName); _ResetPasswordService.AddPasswordHistory(_secUserPasswordHistory); //End // ModelState.AddModelError("", "User has been successfully created.."); return(RedirectToAction("Index", "Home")); // return null; } catch (Exception ex) { // ModelState.AddModelError("", "User already exist.."); } } ModelState.AddModelError("", "User already exist.."); // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult Register(RegisterViewModel model) { //ServiceLayer.Services.ResetPasswordService _ResetPasswordService = new ServiceLayer.Services.ResetPasswordService(); List <smIndustry> newlist = _manageContentService.GetCategories(); ViewBag.IndustryName = new SelectList(newlist, "IndustryId", "IndustryName"); if (ModelState.IsValid) { try { //string name = model.Name; string firstname = model.FirstName; string lastname = model.LastName; string[] names = new string[] { }; //if (name.Contains(" ")) //{ // names = name.Split(' '); // firstname = names[0]; // lastname = names[1]; //} List <Core.Domain.Users> list = _userService.GetUsersByEmail(model.UserName.ToString()); int _userID = WebSecurity.GetUserId(model.UserName); if (list.Count == 0 && _userID > 0) { ((SimpleMembershipProvider)Membership.Provider).DeleteUser(model.UserName.ToString(), true); // deletes record from webpages_Membership table } WebSecurity.CreateUserAndAccount(model.UserName, model.Password, propertyValues: new { FirstName = firstname, LastName = lastname, UserType = 2 }); //TODO This Code Use For Mainain Password History string RetPassword = HashData(model.Password); SecUserPasswordHistory _secUserPasswordHistory = new SecUserPasswordHistory(); byte[] array = Encoding.ASCII.GetBytes(RetPassword); _secUserPasswordHistory.PasswordHash256 = array; _secUserPasswordHistory.DeleteFlag = false; _secUserPasswordHistory.RowVersion = null; _secUserPasswordHistory.SecUserID = WebSecurity.GetUserId(model.UserName); _userService.AddPasswordHistory(_secUserPasswordHistory); //End ModelState.AddModelError("", CustomMessages.UserRegSuccess); //// Create mail body //StringBuilder mailBody = new StringBuilder(); //// mailBody.AppendFormat("Registration Email"); //mailBody.AppendFormat("<br />"); //mailBody.AppendFormat("Dear {0}", model.UserName); //mailBody.AppendFormat("<br />"); //mailBody.AppendFormat("<p>Welcome to Social Media Broadcast</p>"); //mailBody.AppendFormat("<p>Now you can post to multiple social media at once.</p>"); //mailBody.AppendFormat("<br />"); //mailBody.AppendFormat("<br />"); //mailBody.AppendFormat("Thanks,"); //mailBody.AppendFormat("Social Media Broadcast team"); //mailBody.AppendFormat("<br />"); //CommonFunctions.SendEmail(model.UserName, "Registration Email", Convert.ToString(mailBody)); //TempData["Message"] = "Your account has been registered please login to continue."; TempData["Message"] = true; return(RedirectToAction("Login")); } catch (Exception ex) { TempData["Message"] = false; ModelState.AddModelError("", "User already exist.."); } } TempData["Message"] = false; ModelState.AddModelError("", "User already exist.."); // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult ForgotPassword(ManageUserViewModel model) { if (ModelState.IsValid) { if (model.OldPassword != model.NewPassword) { //ServiceLayer.Services.ResetPasswordService _ResetPasswordService = new ServiceLayer.Services.ResetPasswordService(); var token = ""; string UserName = WebSecurity.CurrentUserName; //check user existance var user = Membership.GetUser(UserName); bool changePasswordSucceeded; changePasswordSucceeded = user.ChangePassword(model.OldPassword, model.NewPassword); if (!changePasswordSucceeded) { return(Content(CustomMessages.CurrentPassNotCorrect)); } if (user == null) { TempData["Message"] = CustomMessages.UserNotExist; } else { //generate password token token = WebSecurity.GeneratePasswordResetToken(UserName); //create url with above token } bool any = _userService.UpdatePassword(UserName, token); bool response = false; if (any == true) { response = WebSecurity.ResetPassword(token, model.NewPassword); if (response == true) { try { // Here Maintain Password History // MembershipUser u = Membership.GetUser(WebSecurity.CurrentUserName, false); string RetPassword = HashData(model.NewPassword); SecUserPasswordHistory _secUserPasswordHistory = new SecUserPasswordHistory(); byte[] array = Encoding.ASCII.GetBytes(RetPassword); _secUserPasswordHistory.PasswordHash256 = array; _secUserPasswordHistory.DeleteFlag = false; _secUserPasswordHistory.RowVersion = null; _secUserPasswordHistory.SecUserID = (WebSecurity.CurrentUserId); _userService.AddPasswordHistory(_secUserPasswordHistory); //TempData["Message"] = CustomMessages.PasswordChanged; return(Content(CustomMessages.PasswordChanged)); } catch (Exception ex) { TempData["Message"] = CustomMessages.ErrorWhileChangingPassword + ex.Message; } } else { TempData["Message"] = CustomMessages.HeyAvoidRandomRequest; } } else { TempData["Message"] = CustomMessages.UserAndTokenNotMatch; } } else { return(Content(CustomMessages.PasswordsMustbeDiff)); } } return(View(model)); }
public ActionResult Register(RegisterViewModel model) { //ServiceLayer.Services.ResetPasswordService _ResetPasswordService = new ServiceLayer.Services.ResetPasswordService(); if (ModelState.IsValid) { try { string name = model.Name; string firstname = string.Empty; string lastname = string.Empty; string[] names = new string[] {}; if (name.Contains(" ")) { names = name.Split(' '); firstname = names[0]; lastname = names[1]; } List <Users> list = _userService.GetUsersByEmail(model.UserName.ToString()); int _userID = WebSecurity.GetUserId(model.UserName); if (list.Count == 0 && _userID > 0) { ((SimpleMembershipProvider)Membership.Provider).DeleteUser(model.UserName.ToString(), true); // deletes record from webpages_Membership table } WebSecurity.CreateUserAndAccount(model.UserName, model.Password, propertyValues: new { FirstName = firstname, LastName = lastname }); //TODO This Code Use For Mainain Password History string RetPassword = HashData(model.Password); SecUserPasswordHistory _secUserPasswordHistory = new SecUserPasswordHistory(); byte[] array = Encoding.ASCII.GetBytes(RetPassword); _secUserPasswordHistory.PasswordHash256 = array; _secUserPasswordHistory.DeleteFlag = false; _secUserPasswordHistory.RowVersion = null; _secUserPasswordHistory.SecUserID = WebSecurity.GetUserId(model.UserName); _userService.AddPasswordHistory(_secUserPasswordHistory); //End ModelState.AddModelError("", CustomMessages.UserRegSuccess); _emailService.SendRegistrationEmail(model.UserName, model.UserName, model.UserName); //string from = "*****@*****.**"; //using (MailMessage mail = new MailMessage(from, model.UserName)) //{ // mail.Subject = CustomMessages.RegNotification; // mail.IsBodyHtml = false; // SmtpClient smtp = new SmtpClient(); // smtp.Host = "smtp.gmail.com"; // smtp.EnableSsl = true; // NetworkCredential networkCredential = new NetworkCredential(from, "Tej@1234"); // smtp.UseDefaultCredentials = true; // smtp.Credentials = networkCredential; // smtp.Port = 587; // smtp.Send(mail); //} return(RedirectToAction("Index", "Home")); } catch (Exception ex) { ModelState.AddModelError("", "User already exist.."); } } ModelState.AddModelError("", "User already exist.."); // If we got this far, something failed, redisplay form return(View(model)); }
public void AddPasswordHistory(SecUserPasswordHistory secUserPasswordHistory) { _userRepository.AddPasswordHistory(secUserPasswordHistory); }