public void DeleteUser(string name) { User user = new User(); user.UserName = name; bool result = user.Delete(); if (result) { frmMain main = (frmMain)Application.OpenForms["frmMain"]; main.Logout(); this.Close(); } }
private void UpdateUser() { if (User.Current != null) { User user = new User(); string message = Language.Resource.Message; if (!string.IsNullOrEmpty(tbxUserName.Text)) { if (_oldPassword == tbxOldPassword.Text) { if (!string.IsNullOrEmpty(tbxNewPassword.Text)) { if (tbxNewPassword.Text == tbxConfirm.Text) { user.UserName = tbxUserName.Text; user.Password = tbxNewPassword.Text; user.Email = tbxEmail.Text; int result = user.Update(); if (result == 1) { frmManager manager = (frmManager)Application.OpenForms["frmManager"]; manager.SelectTab("tabUsers"); this.Close(); } } else { MessageBox.Show(Language.Resource.NewPasswordAndConfirmNotTheSame, message); } } else { MessageBox.Show(Language.Resource.NewPasswordRequire, message); } } else { MessageBox.Show(Language.Resource.OldPasswordIncorrect, message); } } else { MessageBox.Show(Language.Resource.UsernameRequired, message); } } }
public frmUserAccount(string name) { _name = name; _oldPassword = ""; InitializeComponent(); tbxUserName.Enabled = false; if (User.Current != null) { User user = new User(); var current = user.GetBy(name); if (current != null) { tbxUserName.Text = current.UserName; _oldPassword = current.Password; tbxEmail.Text = current.Email; } } }
public int register(string username, string email, string password, string confirm) { int result = 0; User user = new User(); user.UserName = username; user.Email = email; user.Password = password; user.Confirm = confirm; user.Path = Application.StartupPath + "\\" + username + ".tdc"; result = user.Register(); return result; }
public int login(string username, string password) { int result = 0; User user = new User(); user.UserName = username; user.Password = password; user.Path = Application.StartupPath + "\\" + username + ".tdc"; result = user.Login(); if (result == 1) { CurrentUser = user.GetBy(username); ; } else { CurrentUser = null; } return result; }
public int Register() { int result = 0; if (string.IsNullOrEmpty(UserName)) { result = -1; } else if (string.IsNullOrEmpty(Email)) { result = -2; } else if (string.IsNullOrEmpty(Password)) { result = -3; } else if (string.IsNullOrEmpty(Confirm)) { result = -4; } else { string MatchEmailPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@" + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\." + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$"; bool checkEmail = System.Text.RegularExpressions.Regex.IsMatch(Email, MatchEmailPattern); if (!checkEmail) { result = -5; } else { if (Password != Confirm) { result = -6; } else { string publicKey = ""; string privateKey = ""; CspParameters cspParams = null; RSACryptoServiceProvider rsaProvider = null; cspParams = new CspParameters(); cspParams.ProviderType = 1; cspParams.Flags = CspProviderFlags.UseArchivableKey; cspParams.KeyNumber = (int)KeyNumber.Exchange; rsaProvider = new RSACryptoServiceProvider(cspParams); publicKey = rsaProvider.ToXmlString(false); privateKey = rsaProvider.ToXmlString(true); User us = new User(); us.UserName = UserName; us.Email = Email; us.PrivateKey = privateKey; us.PublicKey = publicKey; us.Path = Path; byte[] salt = new byte[16]; byte[] hashPassword = new byte[16]; Security security = new Security(); security.HashPassword(Password, out salt, out hashPassword); string s = Convert.ToBase64String(salt); string p = Convert.ToBase64String(hashPassword); us.Password = p; us.Salt = s; if (!System.IO.File.Exists(Path)) { security.SaveConnectionConfiguration(Path, "test", "test", "test", "test", "test", "test"); security.SaveMailConfiguration(Path, "test", "test", 0, "test", "test"); security.SaveAccountsConfiguration(Path, "test", "test", "test", "test"); security.SaveUserConfiguration(Path, UserName, Email, p, privateKey, publicKey, s); Login(); result = 1; } else { XmlDocument users = security.ReadUserConfiguration(Path); bool check = CheckUserAndEmailExists(UserName, Email, users); if (!check) { XmlNode node = users.SelectSingleNode("/root/users"); XmlNode userNode = users.CreateElement("user"); node.AppendChild(userNode); XmlNode usernameNode = users.CreateElement("username"); usernameNode.AppendChild(users.CreateTextNode(UserName)); userNode.AppendChild(usernameNode); XmlNode emailNode = users.CreateElement("email"); emailNode.AppendChild(users.CreateTextNode(Email)); userNode.AppendChild(emailNode); XmlNode passNode = users.CreateElement("password"); passNode.AppendChild(users.CreateTextNode(p)); userNode.AppendChild(passNode); XmlNode publickeyNode = users.CreateElement("publickey"); publickeyNode.AppendChild(users.CreateCDataSection(PublicKey)); userNode.AppendChild(publickeyNode); XmlNode privateNode = users.CreateElement("privatekey"); privateNode.AppendChild(users.CreateCDataSection(PrivateKey)); userNode.AppendChild(privateNode); XmlNode saltNode = users.CreateElement("salt"); saltNode.AppendChild(users.CreateCDataSection(s)); userNode.AppendChild(saltNode); security.SaveUserConfiguration(Path, users.InnerXml); Login(); result = 1; } else { result = -7; } } } } } return result; }
public int Login() { int result = 0; if (string.IsNullOrEmpty(UserName)) { result = -2; } else if (string.IsNullOrEmpty(Password)) { result = -3; } else { if (System.IO.File.Exists(Path)) { Security security = new Security(); XmlDocument doc = security.ReadUserConfiguration(Path); string _username = ""; string _password = ""; string _email = ""; string _publickey = ""; string _privatekey = ""; string _path = ""; string _salt = ""; foreach (XmlNode node in doc.SelectNodes("root/users/user")) { if (node.SelectSingleNode("username") != null && node.SelectSingleNode("password") != null) { _username = node.SelectSingleNode("username").InnerText; _password = node.SelectSingleNode("password").InnerText; _salt = node.SelectSingleNode("salt").InnerText; if (UserName == _username && security.CheckPassword(Password, Convert.FromBase64String(_salt), Convert.FromBase64String(_password))) { User us = new User(); _email = node.SelectSingleNode("email").InnerText; _publickey = node.SelectSingleNode("publickey").InnerText; _privatekey = node.SelectSingleNode("privatekey").InnerText; _path = Path; us.UserName = _username; us.Email = _email; us.PublicKey = _publickey; us.PrivateKey = _privatekey; us.Path = _path; _username = null; _password = null; _email = null; _publickey = null; _privatekey = null; UserName = null; Password = null; User.Current = us; result = 1; break; } else { result = -4; } } } } else { result = -3; } } return result; }
public string GetByJSON(string name) { string result = ""; User user = new User(); if (Current != null) { Security security = new Security(); XmlDocument users = security.ReadUserConfiguration(User.Current.Path); XmlNode node = users.SelectSingleNode("/root/users/user[username='******']"); if (node != null) { user.UserName = node.SelectSingleNode("username").InnerText; user.Password = node.SelectSingleNode("password").InnerText; user.Email = node.SelectSingleNode("email").InnerText; user.PublicKey = node.SelectSingleNode("publickey").InnerText; user.PrivateKey = node.SelectSingleNode("privatekey").InnerText; } result = new JavaScriptSerializer().Serialize(user); } return result; }
public User GetBy(string name) { User user = new User(); if (Current != null) { Security security = new Security(); XmlDocument users = security.ReadUserConfiguration(User.Current.Path); XmlNode node = users.SelectSingleNode("/root/users/user[username='******']"); if (node != null) { user.UserName = node.SelectSingleNode("username").InnerText; user.Password = node.SelectSingleNode("password").InnerText; user.Email = node.SelectSingleNode("email").InnerText; user.PublicKey = node.SelectSingleNode("publickey").InnerText; user.PrivateKey = node.SelectSingleNode("privatekey").InnerText; } } else { user = null; } return user; }
public string GetAllJSON() { string result = ""; List<User> lstUser = new List<User>(); if (User.Current != null) { Security security = new Security(); XmlDocument users = security.ReadUserConfiguration(User.Current.Path); foreach (System.Xml.XmlNode node in users.SelectNodes("root/users/user")) { User user = new User(); user.UserName = node.SelectSingleNode("username").InnerText; user.Password = node.SelectSingleNode("password").InnerText; user.PublicKey = node.SelectSingleNode("publickey").InnerText; user.PrivateKey = node.SelectSingleNode("privatekey").InnerText; user.Email = node.SelectSingleNode("email").InnerText; lstUser.Add(user); } result = new JavaScriptSerializer().Serialize(lstUser); } return result; }
public List<User> GetAll() { List<User> lstUser = new List<User>(); if (User.Current != null) { Security security = new Security(); XmlDocument users = security.ReadUserConfiguration(User.Current.Path); foreach (System.Xml.XmlNode node in users.SelectNodes("root/users/user")) { User user = new User(); user.UserName = node.SelectSingleNode("username").InnerText; user.Password = node.SelectSingleNode("password").InnerText; user.PublicKey = node.SelectSingleNode("publickey").InnerText; user.PrivateKey = node.SelectSingleNode("privatekey").InnerText; user.Email = node.SelectSingleNode("email").InnerText; lstUser.Add(user); } } return lstUser; }
public int login(string username, string password) { int result = 0; User user = new User(); user.UserName = username; user.Password = password; user.Path = Application.StartupPath + "\\" + username + ".tdc"; result = user.Login(); return result; }
private void LoadUser() { User user = new User(); List<User> listUser = user.GetAll(); Security security = new Security(); lvUsers.Items.Clear(); foreach (User usr in listUser) { ListViewItem item = new ListViewItem(new[] { usr.UserName, usr.Email, security.encrypt(User.Current.PublicKey, usr.Password), usr.PublicKey, usr.PrivateKey }); lvUsers.Items.Add(item); } }