示例#1
0
        public ActionResult ForgotPassword(ForgotPasswordViewModels forgotPasswordViewModels)
        {
            if (!ModelState.IsValid)
            {
                return(View(forgotPasswordViewModels));
            }

            var    newPass = new AccountModels().RandomString(10);
            string pwd     = AccountModels.Encrypt(newPass, true);
            var    subject = "Reset Password";
            var    content = "Your new password is: " + newPass;

            if (forgotPasswordViewModels.AccountType == RoleIds.Customer)
            {
                var findMail = _context.Cutomers.SingleOrDefault(e => e.Email == forgotPasswordViewModels.Email);
                if (findMail == null)
                {
                    ModelState.AddModelError("Email", "Email account does not coincide with registration!");
                }
                else
                {
                    findMail.Account.PasswordHash = pwd;
                    if (new AccountModels().UpdatePassword(findMail.Account))
                    {
                        var sendMail = SendMail(forgotPasswordViewModels.Email, subject, content);
                        ViewBag.Success = true;
                        return(View(forgotPasswordViewModels));
                    }
                }
            }
            else if (forgotPasswordViewModels.AccountType == RoleIds.Seller)
            {
                var findMail = _context.Sellers.SingleOrDefault(e => e.Email == forgotPasswordViewModels.Email);
                if (findMail == null)
                {
                    ModelState.AddModelError("Email", "Email account does not coincide with registration!");
                }
                else
                {
                    findMail.Account.PasswordHash = pwd;
                    if (new AccountModels().UpdatePassword(findMail.Account))
                    {
                        var sendMail = SendMail(forgotPasswordViewModels.Email, subject, content);
                        ViewBag.Success = true;
                        return(View(forgotPasswordViewModels));
                    }
                }
            }
            return(View(forgotPasswordViewModels));
        }
        public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModels model)
        {
            if (ModelState.IsValid)
            {
                var user = await userManager.FindByEmailAsync(model.Email);

                if (user != null && await userManager.IsEmailConfirmedAsync(user))
                {
                    var token = await userManager.GeneratePasswordResetTokenAsync(user);

                    var passwordResetLink = Url.Action("ResetPassword", "Account", new { email = user.Email, token = token }, Request.Scheme);

                    // WriteAllText creates a file, writes the specified string to the file,
                    // and then closes the file.    You do NOT need to call Flush() or Close().
                    System.IO.File.WriteAllText(@"C:\Users\Silviu Mihai\Documents\CsharpTokenUsers\TokenResetUsers.txt", passwordResetLink);

                    return(View("ForgotPasswordConfirmation"));
                }
                return(View("ForgotPasswordConfirmation"));
            }
            return(View());
        }