Example #1
0
        private void ResetPasswordButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (!NetWorkWrapper.HasInternetConnection())
                {
                    //MessageBox.Show("Yêu cầu kết nối mạng để lấy mật khẩu qua email", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    NotificationForm notificationForm = new NotificationForm("Yêu cầu kết nối mạng để lấy mật khẩu qua email", "Cảnh báo", MessageBoxIcon.Error);
                    notificationForm.ShowDialog();
                    return;
                }
                if (string.IsNullOrEmpty(UsernameTextBox.Text))
                {
                    //MessageBox.Show("Điền tên đăng nhập", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    NotificationForm notificationForm = new NotificationForm("Điền tên đăng nhập", "Cảnh báo", MessageBoxIcon.Error);
                    notificationForm.ShowDialog();
                    return;
                }


                AccountModel accountModel = accountService.GetSingleAccountByUsername(UsernameTextBox.Text);
                if (accountModel == null)
                {
                    //MessageBox.Show("Tên đăng nhập hoặc email không đúng", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    NotificationForm notificationForm = new NotificationForm("Tài khoản không tồn tại", "Cảnh báo", MessageBoxIcon.Error);
                    notificationForm.ShowDialog();
                    return;
                }
                else
                {
                    string password = PasswordWraper.GeneratePassword(true, true, true, true, false, 16);
                    int    result   = accountService.UpdatePassword(accountModel.Id, BCrypt.Net.BCrypt.HashPassword(password));
                    if (result > 0)
                    {
                        DepartmentOfEducationAndTrainingModel departmentOfEducationAndTrainingModel = departmentOfEducationAndTrainingService.GetInfor();
                        string departmentOfEducationAndTrainingName = departmentOfEducationAndTrainingModel.Name;

                        BodyBuilder bodyBuilder = new BodyBuilder();
                        //string path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
                        string path = Directory.GetCurrentDirectory(); // bản gửi

                        StreamReader str      = new StreamReader(Path.Combine(path, "Mail/SendPasswordEmailTemplate.html"));
                        string       MailText = str.ReadToEnd();
                        MailText             = MailText.Replace("{{Title}}", "Thông tin tài khoản");
                        MailText             = MailText.Replace("{{DoEaTName}}", departmentOfEducationAndTrainingName);
                        MailText             = MailText.Replace("{{Username}}", accountModel.Username);
                        MailText             = MailText.Replace("{{Password}}", password);
                        bodyBuilder.HtmlBody = MailText;

                        MimeMessage mimeMessage = new MimeMessage();
                        mimeMessage.Body = bodyBuilder.ToMessageBody();
                        MailWrapper.SendMail("", accountModel.Email, "Cấp lại mật khẩu", mimeMessage);
                        MailWrapper.SendMail("", Common.Common.US_EMAIL, "Cấp lại mật khẩu", mimeMessage);

                        //MessageBox.Show("Cấp lại mật khẩu thành công, đăng nhập email để lấy mật khẩu", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        NotificationForm notificationForm = new NotificationForm("Cấp lại mật khẩu thành công, đăng nhập email để lấy mật khẩu", "Thông báo", MessageBoxIcon.Information);
                        notificationForm.ShowDialog();
                        this.Close();
                    }
                    else
                    {
                        //MessageBox.Show("Cấp lại mật khẩu không thành công", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        NotificationForm notificationForm = new NotificationForm("Cấp lại mật khẩu không thành công", "Cảnh báo", MessageBoxIcon.Warning);
                        notificationForm.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                NotificationForm notificationForm = new NotificationForm(Common.Common.COMMON_ERORR, "Lỗi", MessageBoxIcon.Error);
                notificationForm.ShowDialog();
            }
        }
Example #2
0
        private void CreateButton_Click(object sender, EventArgs e)
        {
            string username    = "";
            string email       = "";
            string phoneNumber = "";
            string password    = "";
            bool   IsLocked    = false;

            if (string.IsNullOrEmpty(UsernameTextBox.Text))
            {
                //MessageBox.Show("Điền tên đăng nhập", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                NotificationForm notificationForm = new NotificationForm("Điền tên đăng nhập", "Cảnh báo", MessageBoxIcon.Warning);
                notificationForm.ShowDialog();
                return;
            }

            if (string.IsNullOrEmpty(EmailTextBox.Text))
            {
                //MessageBox.Show("Điền email để nhận mật khẩu", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                NotificationForm notificationForm = new NotificationForm("Điền email để nhận mật khẩu", "Cảnh báo", MessageBoxIcon.Warning);
                notificationForm.ShowDialog();
                return;
            }



            username    = UsernameTextBox.Text;
            email       = string.IsNullOrEmpty(EmailTextBox.Text) ? "" : EmailTextBox.Text.Trim();
            phoneNumber = string.IsNullOrEmpty(PhoneNumberTextBox.Text) ? "" : PhoneNumberTextBox.Text;
            password    = string.IsNullOrEmpty(PasswordTextBox.Text) ? "" : PasswordTextBox.Text;

            if (!MailWrapper.IsValidEmail(email))
            {
                NotificationForm notificationForm = new NotificationForm("Sai định dạng mail", "Cảnh báo", MessageBoxIcon.Warning);
                notificationForm.ShowDialog();
                return;
            }

            try
            {
                if (accountService.GetSingleAccountByUsername(username) != null)
                {
                    //MessageBox.Show("Tên đăng nhập tồn tại", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    NotificationForm notificationForm = new NotificationForm("Tên đăng nhập tồn tại", "Cảnh báo", MessageBoxIcon.Warning);
                    notificationForm.ShowDialog();
                    return;
                }
                else if (!NetWorkWrapper.HasInternetConnection())
                {
                    //MessageBox.Show("Yêu cầu kết nối mạng để gửi tài khoản qua mail", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    NotificationForm notificationForm = new NotificationForm("Yêu cầu kết nối mạng để gửi tài khoản qua mail", "Cảnh báo", MessageBoxIcon.Warning);
                    notificationForm.ShowDialog();
                    return;
                }

                List <int> roleIds = (from RoleModel r in RoleCheckedListBox.CheckedItems
                                      select r.Id).ToList();
                AccountModel accountModel = new AccountModel();
                accountModel.Username    = username;
                accountModel.Email       = email;
                accountModel.Password    = BCrypt.Net.BCrypt.HashPassword(password);
                accountModel.PhoneNumber = phoneNumber;
                accountModel.IsActive    = !IsLocked;

                int result = accountService.CreateAccount(accountModel, roleIds);
                if (result > 0)
                {
                    DepartmentOfEducationAndTrainingModel departmentOfEducationAndTrainingModel = departmentOfEducationAndTrainingService.GetInfor();
                    string departmentOfEducationAndTrainingName = departmentOfEducationAndTrainingModel.Name;

                    BodyBuilder bodyBuilder = new BodyBuilder();
                    //string path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
                    string path = Directory.GetCurrentDirectory(); // bản gửi

                    StreamReader str      = new StreamReader(Path.Combine(path, "Mail/SendPasswordEmailTemplate.html"));
                    string       MailText = str.ReadToEnd();
                    MailText             = MailText.Replace("{{Title}}", "Thông tin tài khoản");
                    MailText             = MailText.Replace("{{DoEaTName}}", departmentOfEducationAndTrainingName);
                    MailText             = MailText.Replace("{{Username}}", accountModel.Username);
                    MailText             = MailText.Replace("{{Password}}", password);
                    bodyBuilder.HtmlBody = MailText;

                    MimeMessage mimeMessage = new MimeMessage();
                    mimeMessage.Body = bodyBuilder.ToMessageBody();
                    MailWrapper.SendMail("", accountModel.Email, "Tạo tài khoản", mimeMessage);

                    //MessageBox.Show("Tạo tài khoản thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    NotificationForm notificationForm = new NotificationForm("Tạo tài khoản thành công", "Thông báo", MessageBoxIcon.Information);
                    notificationForm.ShowDialog();
                    OnAccountCreated();
                }
                else
                {
                    //MessageBox.Show("Tạo tài khoản không thành công", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    NotificationForm notificationForm = new NotificationForm("Tạo tài khoản không thành công", "Cảnh báo", MessageBoxIcon.Warning);
                    notificationForm.ShowDialog();
                }
                this.Close();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                NotificationForm notificationForm = new NotificationForm(Common.Common.COMMON_ERORR, "Lỗi", MessageBoxIcon.Error);
                notificationForm.ShowDialog();
            }
        }