Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        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));
        }