Esempio n. 1
0
        public async Task <ActionResult> ForgotPassword(AccountViewModels.ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByNameAsync(model.Email);

                if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
                {
                    // Don't reveal that the user does not exist or is not confirmed
                    return(View("ForgotPasswordConfirmation"));
                }

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                // string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
                // var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                // await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
                // return RedirectToAction("ForgotPasswordConfirmation", "Account");
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        //[ValidateAntiForgeryToken]
        public async Task <IHttpActionResult> ForgotPassword(AccountViewModels.ForgotPasswordViewModel model)
        {
            try
            {
                using (Entities db = new Entities())
                {
                    var    user            = db.AspNetUsers.FirstOrDefault(usr => usr.Email == model.Email);
                    Random rnd             = new Random();
                    var    uppercaseLetter = ((char)('a' + rnd.Next(0, 26))).ToString().ToUpper() + rnd.Next(1, 9).ToString() + "!@";
                    Guid   newpassword     = Guid.NewGuid();
                    AccountViewModels.ResetPasswordViewModel resetPasswordViewModel = new AccountViewModels.ResetPasswordViewModel();
                    resetPasswordViewModel.Email           = user.ToString();
                    resetPasswordViewModel.Password        = newpassword.ToString() + uppercaseLetter;
                    resetPasswordViewModel.ConfirmPassword = newpassword.ToString() + uppercaseLetter;
                    await ResetPassword(resetPasswordViewModel);

                    return(Ok(true));
                }
            }
            catch (Exception ex)
            {
                return(Ok(false));
            }
        }