}
else if (ChangeInfo)
{
this.Text = "Змінити інформацію користувача";
labelPass1.Text = "Старий пароль:";
labelPass2.Text = "Новий пароль:";
private void buttonSubmit_Click(object sender, EventArgs e)
try
errorProvider1.Clear();
#region Forgets the password
if (checkBoxForgetPass.Enabled && checkBoxForgetPass.Checked)
if (textBoxUsername.Text.Trim() == "")
errorProvider1.SetError(this.textBoxUsername, "Будь-ласка, введіть ім'я користувача");
textBoxUsername.Focus();
return;
var user = Variables.xDocument.Descendants("User").Where(q => q.Attribute("UserName").Value.ToLower() == textBoxUsername.Text.Trim().ToLower());
if (user.Count() < 1)
errorProvider1.SetError(this.textBoxUsername, "Таке ім'я користувача відсутнє у базі даних!!!!");
string password = user.First().Attribute("Password").Value;
NetworkCredential loginInfo = new NetworkCredential("username", "password");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("CyberkillerPS@gmail.ru");
msg.To.Add(new MailAddress(user.First().Attribute("Email").Value));
msg.Subject = "Phonebook Password";
msg.Body = "Yours Password = " + password;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.ru");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
MessageBox.Show("Ваш пароль був відісланий вам на email", "Відправка пароля", MessageBoxButtons.OK, MessageBoxIcon.Information);
catch (Exception ex)
MessageBox.Show(ex.Message, "Помилка", MessageBoxButtons.OK, MessageBoxIcon.Error);
#endregion
#region add new user
else if (this.NewUser)
if (textBoxUsername.Text.Trim() == "" && textBoxUsername.Enabled)
else if (textBoxPassword1.Text.Trim() == "" && textBoxPassword1.Enabled)
errorProvider1.SetError(this.textBoxPassword1, "Будь-ласка, введіть пароль");
else if (textBoxPassword2.Text.Trim() == "" && textBoxPassword2.Enabled)
errorProvider1.SetError(this.textBoxPassword2, "Будь-ласка, введіть підтвердження пароля");
else if (textBoxPassword2.Text.Trim()!= textBoxPassword1.Text.Trim())
errorProvider1.SetError(this.textBoxPassword1, "Ваші паролі повинні співпадати");
errorProvider1.SetError(this.textBoxPassword2, "Ваші паролі повинні співпадати");
else if (textBoxEmail.Text.Trim() == "" && textBoxEmail.Enabled)
errorProvider1.SetError(this.textBoxEmail, "Будь-ласка, введіть правельний Email");
if (!File.Exists(Variables.DBFile))
Variables.xDocument = new XDocument(
new XComment("\n Don't edit manually \n"),
new XElement("PhoneBook",
new XElement("Users",
new XElement("User",
new XAttribute("ID", "01"),
new XAttribute("UserName", textBoxUsername.Text.Trim()),
new XAttribute("Password", textBoxPassword1.Text.Trim()),
new XAttribute("Email", textBoxEmail.Text.Trim()),
new XAttribute("RegDate", DateTime.Now.ToString()))),
new XElement("Settings",
new XElement("Setting",
new XAttribute("UserID", "01"),
new XAttribute("RightToLeft", "NO"),
new XAttribute("Dates", "Persian"),
new XAttribute("FontSize", "10"))),
new XElement("Items")));
Variables.CurrentUserID = "01";
else
Variables.xDocument = XDocument.Parse(TripleDES.DecryptFromFile(Variables.DBFile, TripleDES.ByteKey, TripleDES.IV));
var SameUserQuery = from q in Variables.xDocument.Descendants("User")
where q.Attribute("UserName").Value.ToLower() == textBoxUsername.Text.Trim().ToLower()
select q;
if (SameUserQuery.Count() >= 1)
errorProvider1.SetError(this.textBoxUsername, "Таке ім'я користувача вже існує, Будь-ласка, оберіть інше");
int maxID = 0;
maxID = (from q in Variables.xDocument.Descendants("User")
select (int)q.Attribute("ID")).Max();
catch { }
maxID++;
Variables.CurrentUserID = maxID.ToString();
XElement xElement = new XElement("User",
new XAttribute("ID", maxID),
new XAttribute("RegDate", DateTime.Now.ToString()));
var usersElement = (from q in Variables.xDocument.Descendants("Users")
select q).First();
usersElement.Add(xElement);
xElement = new XElement("Setting",
new XAttribute("UserID", maxID),
new XAttribute("FontSize", "10"));
var settingsElement = (from q in Variables.xDocument.Descendants("Settings")
settingsElement.Add(xElement);
Variables.CurrentUserName = textBoxUsername.Text.Trim();
TripleDES.EncryptToFile(Variables.xDocument.ToString(SaveOptions.DisableFormatting), Variables.DBFile, TripleDES.ByteKey, TripleDES.IV);
//Variables.xDocument.Save("debug.xml");
#region change user
else if (this.ChangeUser)
if (Variables.xDocument == null)
MessageBox.Show("Ваше ім'я користувача та пароль невірні", "Помилка", MessageBoxButtons.OK, MessageBoxIcon.Error);
var query = from q in Variables.xDocument.Descendants("User")
where textBoxUsername.Text.Trim().ToLower() == q.Attribute("UserName").Value.ToLower()
&& textBoxPassword1.Text.Trim().ToLower() == q.Attribute("Password").Value.ToLower()
if (query.Count() == 1)
Variables.CurrentUserID = query.First().Attribute("ID").Value;
Variables.CurrentUserID = "";
Variables.CurrentUserName = "";
#region change info
else if (this.ChangeInfo)
bool changePassword = true;
errorProvider1.SetError(this.textBoxUsername, "Ваше ім'я користувача та пароль невірні");
errorProvider1.SetError(this.textBoxEmail, "Будь-ласка, введіть вірний Email");
else if (textBoxPassword1.Text.Trim() == textBoxPassword2.Text.Trim() && textBoxPassword2.Text.Trim() == "")
changePassword = false;
errorProvider1.SetError(this.textBoxPassword1, "Будь-ласка, введіть старий пароль");
errorProvider1.SetError(this.textBoxPassword2, "Будь-ласка, введіть новий пароль");
var query = (from q in Variables.xDocument.Descendants("User")
where q.Attribute("ID").Value == Variables.CurrentUserID
string oldPassword = query.Attribute("Password").Value;
if (oldPassword.ToLower()!= textBoxPassword1.Text.Trim().ToLower() && changePassword)
errorProvider1.SetError(this.textBoxPassword1, "Старий пароль невірний");
else if (oldPassword == textBoxPassword1.Text.Trim() && changePassword)
query.Attribute("UserName").Value = textBoxUsername.Text.Trim();
query.Attribute("Password").Value = textBoxPassword2.Text.Trim();
query.Attribute("Email").Value = textBoxEmail.Text.Trim();
else if (!changePassword)
Variables.CurrentUserID = query.Attribute("ID").Value;
this.Close();
Variables.CurrentUserID = Variables.CurrentUserName = "";
StackFrame file_info = new StackFrame(true);
Messages.error(ref file_info, ex.Message, this);
Додаток 2: Головне вікно програми
Додаток 3: Форма користувача БД
Додаток 4: Форма створення нового запису в БД
Страницы: 1, 2, 3, 4, 5, 6, 7, 8