private void UpdateUser(string siteId, WebUser user, bool deleteUser) { string rootPath = GetSiteContentPath(siteId); string usersPath = Path.Combine(rootPath, ProtectedUsersFile); // load users file List<string> lines = ReadFile(usersPath); // check if the user already exists List<string> updatedLines = new List<string>(); bool exists = false; for (int i = 0; i < lines.Count; i++) { string line = lines[i]; string updatedLine = line; int colonIdx = line.IndexOf(":"); if (colonIdx != -1) { string username = line.Substring(0, colonIdx); string password = line.Substring(colonIdx + 1); if (String.Compare(username, user.Name, true) == 0) { // already exists exists = true; // check if we need to delete this user if (deleteUser) continue; // change password if required if (!String.IsNullOrEmpty(user.Password)) { // change password BsdDES des = new BsdDES(); password = des.Crypt(user.Password); // update line updatedLine = username + ":" + password; } } } updatedLines.Add(updatedLine); } if (!exists && !deleteUser) { // new user has been added BsdDES des = new BsdDES(); updatedLines.Add(user.Name + ":" + des.Crypt(user.Password)); } // save users file WriteFile(usersPath, updatedLines); if (user.Groups == null) user.Groups = new string[] { }; // update groups // open groups file string groupsPath = Path.Combine(rootPath, ProtectedGroupsFile); List<string> groupLines = ReadFile(groupsPath); for (int i = 0; i < groupLines.Count; i++) { string groupLine = groupLines[i]; int colonIdx = groupLine.IndexOf(":"); if (colonIdx != -1) { string groupName = groupLine.Substring(0, colonIdx); string[] groupMembers = groupLine.Substring(colonIdx + 1).Split(' '); // check if user is assigned to this group bool assigned = false; for (int j = 0; j < user.Groups.Length; j++) { if (String.Compare(user.Groups[j], groupName, true) == 0) { assigned = true; break; } } // remove current user List<string> updatedMembers = new List<string>(); for (int j = 0; j < groupMembers.Length; j++) { // user exists in the members // check if he should be really added to this group if (String.Compare(groupMembers[j], user.Name, true) == 0) continue; updatedMembers.Add(groupMembers[j]); } if (assigned) updatedMembers.Add(user.Name); // modify group line groupLines[i] = groupName + ":" + String.Join(" ", updatedMembers.ToArray()); } } // end iterating groups // save group file WriteFile(groupsPath, groupLines); }
public static string GeneratePasswordHash(HtaccessUser user) { if (HtaccessFolder.AUTH_TYPE_BASIC == user.AuthType) { // BASIC switch (user.EncType) { case HtaccessUser.ENCODING_TYPE_APACHE_MD5: return PasswdHelper.MD5Encode(user.Password, PasswdHelper.GetRandomSalt()); case HtaccessUser.ENCODING_TYPE_SHA1: return PasswdHelper.SHA1Encode(user.Password); case HtaccessUser.ENCODING_TYPE_UNIX_CRYPT: default: BsdDES bsdDes = new BsdDES(); return bsdDes.Crypt(user.Password); } } // DIGEST return string.Format("{0}:{1}", user.Realm, PasswdHelper.DigestEncode(user.Name, user.Password, user.Realm)); }