示例#1
0
        protected void btnForgotPassword_Click(object sender, EventArgs e)
        {
            string          email = txtUserEmail.Text;
            DataAccessLayer dao   = new DataAccessLayer();

            if (dao.CheckUserExists(email) == true)
            {
                var chars       = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
                var stringChars = new char[50];
                var random      = new Random();

                for (int i = 0; i < stringChars.Length; i++)
                {
                    stringChars[i] = chars[random.Next(chars.Length)];
                }

                string finalString = new String(stringChars);

                if (dao.InsertPasswordResetLink(email, finalString) == true)
                {
                    MailAddress to   = new MailAddress(email);
                    MailAddress from = new MailAddress(Constants.emailID);

                    string baseUrl  = Request.Url.GetLeftPart(UriPartial.Authority);
                    string ResetUrl = baseUrl + "/ResetPassword.aspx?resetID=" + finalString + "&userID=" + email;

                    string encodedResetUrl = new Uri(ResetUrl).AbsoluteUri;

                    string link = "<a href='" + encodedResetUrl + "'>" + encodedResetUrl + "</a>";

                    MailMessage message = new MailMessage(from, to);
                    message.Subject = "Password Reset - Kent Homes";
                    message.Body    = "<p>Click the below to reset the password<p>" + link;

                    message.IsBodyHtml = true;

                    SmtpClient client = new SmtpClient(Constants.serverAddress, Constants.emailPort)
                    {
                        Credentials = new NetworkCredential(Constants.emailID, Constants.emailPassword),
                        EnableSsl   = true
                    };
                    // code in brackets above needed if authentication required

                    try
                    {
                        client.Send(message);
                    }
                    catch (SmtpException ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                    responseForgotPassword.Text = "Password Reset Link sent to Email";
                }
            }
            else
            {
                responseForgotPassword.Text = "UserID doesn't Exists!";
            }
        }