/// <summary> /// Check if the password exists or not, /// because two users are not allowed to have same password. /// </summary> /// <param name="password">just the password</param> /// <returns></returns> public bool CheckPasswordExists(string password) { HarshCodeConverter hashcode = new HarshCodeConverter(); if (m_UserInfoDatabase == null) { LoadCredentialFile(); } try { for (int i = 0; i < m_UserInfoDatabase.Count; ++i) { string saltedPassword = password + m_UserInfoDatabase[i].m_SaltTag; string hashedPassword = hashcode.Convert(saltedPassword); if (m_UserInfoDatabase[i].m_Password == hashedPassword) { return true; } } } catch { } return false; }
/// <summary> /// Constructor of CredentialFile class /// </summary> public CredentialFile() { // Generate random file name string salt = new GenerateSalt().RandomSalt(); m_FullFilePath = "UserAccount_" + salt + ".txt"; m_UserInfoDatabase = new List<UserInfo>(); if (!File.Exists(m_FullFilePath)) { FileStream fs = File.Create(m_FullFilePath); fs.Close(); } else { LoadCredentialFile(); } HarshCodeConverter hashcode = new HarshCodeConverter(); //UserInfo Admin = new UserInfo("Admin", hashcode.Convert("111111111111abc"), "abc"); UserInfo Admin = new UserInfo("team1", hashcode.Convert("clientapiabc"),"abc"); Admin.m_Admin = true; m_UserInfoDatabase.Add(Admin); }
/// <summary> /// Used for log in to authenticate a user /// </summary> /// <param name="username">username of the user</param> /// <param name="password">password of the user</param> /// <returns></returns> public bool UserAuthenticate(string username, string password) { HarshCodeConverter hashcode = new HarshCodeConverter(); if (m_UserInfoDatabase == null) { LoadCredentialFile(); } for (int i = 0; i < m_UserInfoDatabase.Count; ++i) { if (m_UserInfoDatabase[i].m_UserName == username) { string saltedPassword = password + m_UserInfoDatabase[i].m_SaltTag; string hashedPassword = hashcode.Convert(saltedPassword); if (m_UserInfoDatabase[i].m_Password == hashedPassword) { return true; } } } return false; }