示例#1
0
        public IHttpActionResult Recover(PasswordRecoverModel passwordRecoverModel)
        {
            if (string.IsNullOrEmpty(passwordRecoverModel.Email) && string.IsNullOrEmpty(passwordRecoverModel.UserName))
            {
                return(BadRequest(SpongeSolutions.ServicoDireto.Internationalization.Message.Email_or_UserName_Empty));
            }

            RegisterModel user = null;

            if (!string.IsNullOrEmpty(passwordRecoverModel.UserName))
            {
                user = Services.ServiceContext.AccountMembershipService.GetUserByName(passwordRecoverModel.UserName, 0, 1).FirstOrDefault();
            }
            else
            {
                user = Services.ServiceContext.AccountMembershipService.GetUserByEmail(passwordRecoverModel.Email, 0, 1).FirstOrDefault();
            }

            if (user == null)
            {
                return(BadRequest(SpongeSolutions.ServicoDireto.Internationalization.Message.User_Not_Found));
            }

            if (passwordRecoverModel.Recovery == RecoveryType.Username)
            {
                this.SendEmailUserNameRecover(passwordRecoverModel.IDCulture, user);
                return(Ok(new { Message = Internationalization.Label.Access_Data_Recovered }));
            }
            else
            {
                this.SendEmailPasswordRecover(passwordRecoverModel.IDCulture, Services.ServiceContext.CustomerService.GetByUserEmail(user.Email));
                return(Ok(new { Message = Internationalization.Message.Email_Password_Recover_Sent }));
            }
        }
示例#2
0
        public ActionResult RecoverPassword(PasswordRecoverModel Model)
        {
            if (Model.Email == null || Model.Name == null)
            {
                ModelState.AddModelError("MyObject", "Заполните все поля");
                return(View());
            }
            UsersContext      db        = new UsersContext();
            EmailModel        FindUser  = new EmailModel();
            List <EmailModel> _tempList = new List <EmailModel>();

            _tempList = db.EmailModels.ToList();
            for (int i = 0; i < _tempList.Count; i++)
            {
                if (_tempList[i].Email.Trim().ToLower() == Model.Email.Trim().ToLower() &&
                    _tempList[i].UserProfile.UserName.Trim().ToLower() == Model.Name.Trim().ToLower())
                {
                    FindUser = _tempList[i];
                    break;
                }
            }
            if (FindUser.Email == null)
            {
                ModelState.AddModelError("MyObject", "Данные введены не корректно");
                return(View());
            }

            string Key = WebSecurity.GeneratePasswordResetToken(FindUser.UserProfile.UserName);

            SendMail("smtp.mail.ru", "*****@*****.**", "7632bxr29zx6", FindUser.Email, "public", "Welcom to public " +
                     "Для восстановления пароля перейдите по этой ссылке http://public.somee.com/Account/RecoverPasswordPage?Key=" + Key, null);

            return(View());
        }
示例#3
0
        public async Task <bool> SetPassword(PasswordRecoverModel model)
        {
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                throw new Exception("Cannot find the email");
            }
            var result = await _userManager.ResetPasswordAsync(user, model.Token, model.NewPassword);

            return(result.Succeeded);
        }
        public async Task <IHttpActionResult> SetPassword(PasswordRecoverModel user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var result = await _repo.SetPassword(user);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            return(Ok());
        }
示例#5
0
        public ActionResult Recover(PasswordRecoverModel passwordRecoverModel)
        {
            if (string.IsNullOrEmpty(passwordRecoverModel.Email) && string.IsNullOrEmpty(passwordRecoverModel.UserName))
            {
                ModelState.AddModelError("", SpongeSolutions.ServicoDireto.Internationalization.Message.Email_Empty);
            }
            else
            {
                RegisterModel user = null;
                if (!string.IsNullOrEmpty(passwordRecoverModel.UserName))
                {
                    user = MembershipService.GetUserByName(passwordRecoverModel.UserName, 0, 1).FirstOrDefault();
                }
                else
                {
                    user = MembershipService.GetUserByEmail(passwordRecoverModel.Email, 0, 1).FirstOrDefault();
                }

                this.SendEmailUserNameRecover(user);
                this.TempData["Message"] = Internationalization.Label.Access_Data_Recovered;
            }
            return(View(passwordRecoverModel));
        }
 public async Task <IdentityResult> SetPassword(PasswordRecoverModel user)
 {
     return(await _userManager.ResetPasswordAsync(user.UserId, user.Token, user.NewPassword));
 }
示例#7
0
        public async Task <IActionResult> PasswordRecoverAsync(PasswordRecoverModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    SetTitle("Khôi phục mật khẩu", "password-recover");
                    ModelState.AddModelError(string.Empty, string.Join(", ", GetModelStateErrors()));
                    return(View("PasswordRecover", model));
                }

                var user = await _userService.GetByEmailAsync(model.Email);

                if (user == null)
                {
                    SetTitle("Khôi phục mật khẩu", "password-recover");
                    ModelState.AddModelError(string.Empty, "Email không tồn tại trong hệ thống");
                    return(View("PasswordRecover", model));
                }

                //generate new password
                var newPassword = Common.RandomString(8);

                //update user's password
                user.Password = Crypto.HashPassword(newPassword);
                var response = await _userService.SaveAsync(new SaveRequest <Entity.User>
                {
                    Entity = user,
                    IsEdit = true,
                    UserId = user.Id
                });

                //send mail with new password
                if (response.Success)
                {
                    var path         = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "emailtemplate", "recovery-password.html");
                    var htmlTemplate = System.IO.File.ReadAllText(path);
                    htmlTemplate = htmlTemplate.Replace("{{FullName}}", user.FullName)
                                   .Replace("{{NewPassword}}", newPassword);

                    await EmailHelper.SendEmailAsync(
                        Common.DefaultConfig,
                        new EmailMessage
                    {
                        From    = Common.DefaultConfig.SmtpUsername,
                        To      = model.Email,
                        Subject = "[NO REPLY] KHÔI PHỤC MẬT KHẨU",
                        Content = htmlTemplate
                    });

                    SetTitle("Khôi phục mật khẩu", "password-recover");
                    return(View("PasswordRecovered", new PasswordRecoveredModel
                    {
                        AvatarUrl = user.AvatarUrl,
                        FullName = user.FullName
                    }));
                }

                SetTitle("Khôi phục mật khẩu", "password-recover");
                ModelState.AddModelError(string.Empty, "Đã xảy ra sự cố. Vui lòng thử lại.");
                return(View("PasswordRecover", model));
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "AccountController > PasswordRecoverAsync");
                return(NotFound());
            }
        }