예제 #1
0
        public async Task <ActionResult> SendNewPassword(LoginViewModel model)
        {
            var user = await UserManager.FindByNameAsync(model.UserName);

            if (user != null)
            {
                int userType = uow.TraderRepo.FindTraderById(user.TraderId).TypeFlag;
                if (user != null && userType != 1)
                {
                    if (!user.IsVerified || !user.IsActive)
                    {
                        ModelState.AddModelError("", !user.IsVerified ? Resources.Messages.VerifyAccount : Resources.Messages.Userinactive);
                        return(View("ForgetPassword", model));
                    }
                    string         destination = user.Email;
                    string         generatedPW = UsefulMethods.GenerateNewPassword();
                    PasswordHasher hasher      = new PasswordHasher();
                    string         hashed      = hasher.HashPassword(generatedPW);
                    //user.DefaultPassword = generatedPW;
                    user.PasswordHash      = null;
                    user.Token             = Guid.NewGuid();
                    user.NeedReset         = true;
                    user.TokenCreationDate = DateTime.Now;
                    UserManager.Update(user);

                    model.Resend   = false;
                    model.UserName = General.CheckMail;
                    //send new password mail .

                    var  callbackUrl = Url.Action("ForgotPassword", "Account", new { userName = user.UserName, code = user.Token }, protocol: Request.Url.Scheme);
                    Mail objMail     = new Mail {
                        Destination = user.Email, Subject = "Forget Password", Body = GetBodyContent("كلمة السر الجديدة", "Please click this link to set your new password: <a href=\"" + callbackUrl + "\">link</a>")
                    };
                    await UsefulMethods.SendAsync(objMail);

                    return(View("DisplayEmail", model));
                }
                else
                {
                    ModelState.AddModelError("", userType == 1 ? Resources.Messages.SuperTraderType : Resources.Messages.UserInCorrect);
                    return(View("ForgetPassword", model));
                }
            }
            else
            {
                ModelState.AddModelError("", user == null ? Resources.Messages.verifyUserName : Resources.Messages.userNotExsist);
                return(View("ForgetPassword", model));
            }
        }