Exemplo n.º 1
0
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            lblInvalidEmail.Text = "";
            lblConfirmPassword.Text = "";
            string option = "";
            double num = 0;

            Lecturer lecturer = new Lecturer();

            lecturer.Email = txtEmail.Value;
            lecturer.Password = txtPassword.Value;
            lecturer.FirstName = txtFirstName.Value;
            lecturer.Surname = txtSurname.Value;

            LecturerHandler lecturerHandler = new LecturerHandler();

            if (txtPassword.Value == txtConfirmPassword.Value)
            {
                if (lecturerHandler.ValidateEmail(txtEmail.Value) == false/* && double.TryParse(option, out num) == true*/)
                {
                    if (lecturerHandler.AddNewLecturer(lecturer) == false)
                        Response.Redirect("Login.aspx?registered=1");
                }

                else if (lecturerHandler.ValidateEmail(txtEmail.Value) == true)
                {
                    lblInvalidEmail.Text = "This E-Mail address is already in use";
                    txtPassword.Attributes.Add("value", txtPassword.Value);
                    txtConfirmPassword.Attributes.Add("value", txtConfirmPassword.Value);
                }

                else if (double.TryParse(option, out num) == false)
                {
                    lblInvalidEmail.Text = "";
                    txtPassword.Attributes.Add("value", txtPassword.Value);
                    txtConfirmPassword.Attributes.Add("value", txtConfirmPassword.Value);
                }
            }
            else
            {
                lblConfirmPassword.Text = "Passwords do not match";
            }
        }
        protected void btnRecoverPassword_Click(object sender, EventArgs e)
        {
            LecturerHandler lecturerHandler = new LecturerHandler();
            Lecturer lecturer = null;
            BusinessHandler businessHandler = null;
            Business business = null;

            string destinationEmail = txtEmail.Value;

            //check email exists
            if (lecturerHandler.ValidateEmail(destinationEmail) == false)
            {
                //email doesn't exist in DB
                litAlert.Text = "<div class='alert alert-danger'>Invalid Email Address</div>";
            }

            else
            {
                //get business email and password
                string name, email, password, emailServer, newPassword;
                int port;

                businessHandler = new BusinessHandler();
                business = new Business();
                business = businessHandler.GetBusinessDetails();

                name = business.Name;
                email = business.Email;
                password = business.EmailPassword;
                emailServer = business.EmailServer;
                port = business.EmailPort;

                //generate new password
                newPassword = Membership.GeneratePassword(7, 0);

                //update database
                lecturer = new Lecturer();
                lecturer.Email = destinationEmail;
                lecturer.Password = newPassword;

                //send email
                try
                {
                    MailMessage mail = new MailMessage();
                    SmtpClient smtpClient = new SmtpClient(emailServer);
                    mail.From = new MailAddress(email);
                    mail.To.Add(destinationEmail);
                    mail.Subject = name + " Password Reset";
                    mail.Body = "Your password has been reset. Please use the following phrase as your new password when you log in: " + newPassword;

                    //code to include an attatchment
                    //System.Net.Mail.Attachment attachment;
                    //attachment = new System.Net.Mail.Attachment("attatchment.jpg");
                    //mail.Attachments.Add(attachment);

                    smtpClient.Port = port;
                    smtpClient.Credentials = new NetworkCredential(email, password);
                    smtpClient.EnableSsl = true;

                    smtpClient.Send(mail);
                    lecturerHandler.UpdateLecturerPassword(lecturer);

                    litAlert.Text = "<div class='alert alert-success'>An email was sent, check you email for your new password.</div>";

                    //delay redirect to alert user of page change
                    /*lblRedirect.Text = "Redirecting to log in, in 5 seconds.";
                    Response.Write("<script type=\"text/javascript\">setTimeout(function () { window.location.href = \"Login.aspx\"; }, 5000);</script>");*/
                }
                catch (Exception)
                {
                    litAlert.Text = "<div class='alert alert-warning'>Failed to send an email</div>";
                }
            }
        }