コード例 #1
0
        public ActionResult Index(ForgotPassword forgotPassword)
        {
            int userId = (new UserAccess()).getUserId(forgotPassword.Email);

            // if user doesn't exist
            if (userId == 0)
            {
                // pass error message and return the page
                ViewBag.ErrorMsg = "User Account not exists";

                return(View());
            }

            string userName = (new UserAccess()).retreiveUserByUserId(userId).UserName;

            try
            {
                // generate token
                forgotPassword.token = forgotPassword.GenerateRandomString(30);



                // save it to db
                bool isUpdate = (new forgotPasswordTokenAccess()).updateToken(userId, forgotPassword.token);

                // if error exist, when save the detail pass the error message
                if (!isUpdate)
                {
                    ViewBag.ErrorMsg = "Updating Failed";
                    return(View());
                }

                // send it to the user's email account

                Email email = new Email(forgotPassword.Email);


                int isSuccess = email.SendMail("Hi " + userName + " ! <br><br>We recieved a request to reset your password.<br><br> Click here to Reset Your password : <a href='" + string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~")) + "ForgotPassword/ConfirmAccount?userId=" + userId + "&token=" + forgotPassword.token + "'>Link</a><br> If you don't want to change your password, you can ignore this email.<br><br> Thanks,<br> The Futunet Net Team", "Account - Help (Reset Your Password)");

                // if sending failed -- return the page with error
                if (isSuccess == 0)
                {
                    ViewBag.errorMsg = "Sending Mail Failed";
                    return(View());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            // return the page with success
            ViewBag.Message = "Instructions on how to reset Your Password have been sent to your inbox";
            return(View());
        }