Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
0
        public int Update()
        {
            int result = 0;

            if (User.Current != null)
            {
                Security security = new Security();
                XmlDocument users = security.ReadUserConfiguration(User.Current.Path);
                XmlNode node = users.SelectSingleNode("/root/users/user[username='******']");
                if (node != null)
                {
                    node.SelectSingleNode("username").InnerText = UserName;
                    node.SelectSingleNode("password").InnerText = Password;
                    node.SelectSingleNode("email").InnerText = Email;

                    security.SaveUserConfiguration(User.Current.Path, users.OuterXml);

                    result = 1;
                }
            }

            return result;
        }
Ejemplo n.º 3
0
        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;
        }
Ejemplo n.º 4
0
        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;
        }
Ejemplo n.º 5
0
        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;
        }
Ejemplo n.º 6
0
        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;
        }
Ejemplo n.º 7
0
        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;
        }