public static string CreateUserInAD(User user) { if (AD.singleton == null) { AD.singleton = new AD(); } string userDn = string.Empty; try { string month = "OU=" + Enum.GetName(typeof(Month), DateTime.Now.Month); string year = "OU=" + DateTime.Now.Year.ToString(); string connectionString = AD.Host + "/" + AD.UsersOU + "," + AD.BaseDN; string monthString = year + "," + AD.UsersOU + "," + AD.BaseDN; string yearString = AD.UsersOU + "," + AD.BaseDN; new OU(year, yearString); new OU(month, monthString); string userPath = AD.Host + "/" + month + "," + year + "," + AD.UsersOU + "," + AD.BaseDN; DirectoryEntry dirEntry = AD.GetObjectDirectoryEntry(userPath); DirectoryEntry newUser = dirEntry.Children.Add("CN=" + user.UserName, "user"); newUser.Properties["sAMAccountName"].Value = user.UserName; newUser.Properties["employeeID"].Value = user.UIN; newUser.Properties["department"].Value = user.Major; newUser.Properties["company"].Value = user.College; newUser.Properties["title"].Value = user.Title; newUser.Properties["mail"].Value = user.Email; newUser.Properties["telephoneNumber"].Value = user.PhoneNumber; newUser.Properties["givenName"].Value = user.FirstName; newUser.Properties["sn"].Value = user.LastName; newUser.Properties["employeeNumber"].Value = user.MembershipNumber; newUser.Properties["employeeType"].Value = user.MemberType; newUser.Properties["description"].Value = user.OtherData; newUser.CommitChanges(); user.dn = newUser.Properties["distinguishedName"].Value.ToString(); dirEntry.Close(); dirEntry.Dispose(); newUser.Close(); newUser.Dispose(); User.UpdatePasword(user.dn, user.UserPassword); User.Unlock(user.dn); User.Enable(user.dn); Group.AddMember(user.dn, AD.PaidGroup); } catch (System.DirectoryServices.DirectoryServicesCOMException E) { throw E; } return(userDn); }
public static void RemoveMember(string userDn, string groupDn) { try { DirectoryEntry dirEntry = AD.GetObjectDirectoryEntry(groupDn); dirEntry.Properties["member"].Remove(userDn); dirEntry.CommitChanges(); dirEntry.Close(); dirEntry.Dispose(); } catch (DirectoryServicesCOMException E) { throw E; } }
public static void Unlock(string userDn) { try { DirectoryEntry user = AD.GetObjectDirectoryEntry(userDn); user.Properties["LockOutTime"].Value = 0; user.CommitChanges(); user.Close(); user.Dispose(); } catch (DirectoryServicesCOMException E) { throw E; } }
public static void Disable(string userDn) { try { DirectoryEntry user = AD.GetObjectDirectoryEntry(userDn); int val = (int)user.Properties["userAccountControl"].Value; user.Properties["userAccountControl"].Value = val | 0x2; user.CommitChanges(); user.Close(); user.Dispose(); } catch (DirectoryServicesCOMException E) { throw E; } }
public static void UpdatePasword(string userDn, string password) { try { DirectoryEntry user = AD.GetObjectDirectoryEntry(userDn); user.Invoke("SetPassword", new object[] { password }); user.CommitChanges(); user.CommitChanges(); user.Close(); user.Dispose(); } catch (DirectoryServicesCOMException E) { throw E; } }