/// <summary> /// 添加组织单位。 /// </summary> /// <param name="locationPath">组织单位被添加的位置,ADsPath。DN形式。完全转义。</param> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> public void Add(string locationPath, string userName, string password) { if (locationPath.IndexOf(ParaMgr.LDAP_IDENTITY) >= 0) { locationPath = locationPath.Substring(7); } DirectoryEntry parent = null; DirectoryEntry newOU = null; // 默认位置,在域容器下 if (String.IsNullOrEmpty(locationPath)) { locationPath = ParaMgr.ADFullPath; } if (!ADManager.Exists(locationPath)) { throw new EntryNotExistException("指定的位置对象不存在。"); } string rdn = Utils.GenerateRDNOU(this.name); // 使用name做OU // 这里的问题是要求DN形式的的Path if (ADManager.Exists(Utils.GenerateDN(rdn, locationPath))) { throw new EntryNotExistException("指定的位置下存在同名对象。"); } try { parent = ADManager.GetByPath(locationPath, userName, password); newOU = parent.Children.Add(rdn, "organizationalUnit"); Utils.SetProperty(newOU, OU.PROPERTY_DESCRIPTION, this.description); Utils.SetProperty(newOU, OU.PROPERTY_MANAGEDBY, this.managedBy); // 注意,不能是转义/的DN newOU.CommitChanges(); // reload this.Parse(newOU); } catch (DirectoryServicesCOMException dsce) { throw dsce; } finally { if (parent != null) { parent.Close(); parent.Dispose(); } if (newOU != null) { newOU.Close(); newOU.Dispose(); } } }
/// <summary> /// 更改组织单位名称。 /// </summary> /// <param name="newName">该项的新名称。</param> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> public void Rename(string newName, string userName, string password) { DirectoryEntry de = null; string rdn = Utils.GenerateRDNOU(newName); if (ADManager.Exists(Utils.GenerateDN(rdn, Utils.GetParentDN(this.Dn)))) { throw new SameRDNException("已存在同名对象。"); } try { de = ADManager.GetByDN(this.Dn, userName, password); // 必须是DN形式,且完全转义。 de.Rename(rdn); de.CommitChanges(); // Reload this.Parse(de); } catch (DirectoryServicesCOMException dsce) { throw dsce; } finally { if (de != null) { de.Close(); de.Dispose(); } } }
/// <summary> /// 添加组。 /// </summary> /// <param name="locationPath">组被添加的位置,ADsPath。DN形式,完全转义。</param> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> public void Add(string locationPath, string userName, string password) { if (locationPath.IndexOf(ParaMgr.LDAP_IDENTITY) >= 0) { locationPath = locationPath.Substring(7); } DirectoryEntry parent = null; DirectoryEntry newGroup = null; // 默认位置,在Users容器下 if (String.IsNullOrEmpty(locationPath)) { locationPath = "CN=Users," + ParaMgr.ADFullPath; } if (!ADManager.Exists(locationPath)) { throw new EntryNotExistException("指定的位置对象不存在。"); } string rdn = Utils.GenerateRDNCN(this.name); // 使用name做CN // 这里的问题是要求DN形式的的Path if (ADManager.Exists(Utils.GenerateDN(rdn, locationPath))) { throw new EntryNotExistException("指定的位置下存在同名对象。"); } try { parent = ADManager.GetByPath(locationPath, userName, password); newGroup = parent.Children.Add(rdn, "group"); Utils.SetProperty(newGroup, Group.PROPERTY_ACCOUNT, this.accountName); Utils.SetProperty(newGroup, Group.PROPERTY_INFO, this.info); Utils.SetProperty(newGroup, Group.PROPERTY_DESCRIPTION, this.description); Utils.SetProperty(newGroup, Group.PROPERTY_GROUPTYPE, (int)GroupScope.ADS_GROUP_TYPE_GLOBAL_GROUP); newGroup.CommitChanges(); // reload this.Parse(newGroup); } catch (DirectoryServicesCOMException dsce) { throw dsce; } finally { if (parent != null) { parent.Close(); parent.Dispose(); } if (newGroup != null) { newGroup.Close(); newGroup.Dispose(); } } }
/// <summary> /// 添加用户。 /// </summary> /// <param name="locationPath">用户被添加的位置,ADsPath。DN形式,完全转义。</param> /// <param name="newUserPassword">用户的密码</param> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> public void Add(string locationPath, string newUserPassword, string userName, string password) { if (locationPath.IndexOf(ParaMgr.LDAP_IDENTITY) >= 0) { locationPath = locationPath.Substring(7); } DirectoryEntry parent = null; DirectoryEntry newUser = null; // 默认位置,在Users容器下 if (String.IsNullOrEmpty(locationPath)) { locationPath = "CN=Users," + ParaMgr.ADFullPath; } if (!ADManager.Exists(locationPath)) { throw new EntryNotExistException("指定的位置对象不存在。"); } string rdn = Utils.GenerateRDNCN(this.name); // 使用name做CN // 这里的问题是要求DN形式的的Path if (ADManager.Exists(Utils.GenerateDN(rdn, locationPath))) { throw new EntryNotExistException("指定的位置下存在同名对象。"); } try { parent = ADManager.GetByPath(locationPath, userName, password); newUser = parent.Children.Add(rdn, "user"); Utils.SetProperty(newUser, User.PROPERTY_ACCOUNT_SAM, this.userName); Utils.SetProperty(newUser, User.PROPERTY_GENERAL_GIVENNAME, this.firstName); Utils.SetProperty(newUser, User.PROPERTY_GENERAL_LASTNAME, this.lastName); Utils.SetProperty(newUser, User.PROPERTY_GENERAL_INITIALS, this.initials); Utils.SetProperty(newUser, User.PROPERTY_GENERAL_DISPLAYNAME, this.displayName); Utils.SetProperty(newUser, User.PROPERTY_GENERAL_OFFICE, this.office); Utils.SetProperty(newUser, User.PROPERTY_ORGAN_TITLE, this.title); Utils.SetProperty(newUser, User.PROPERTY_ORGAN_MANAGER, this.manager); // 注意,不能是转义/的DN Utils.SetProperty(newUser, User.PROPERTY_ORGAN_DEPARTMENT, this.department); Utils.SetProperty(newUser, User.PROPERTY_GENERAL_TEL, this.telephone); Utils.SetProperty(newUser, User.PROPERTY_TEL_MOBILE, this.mobile); Utils.SetProperty(newUser, User.PROPERTY_GENERAL_MAIL, this.mail); Utils.SetProperty(newUser, User.PROPERTY_ACCOUNT_PRINCIPAL, this.principalName); Utils.SetProperty(newUser, User.PROPERTY_ACCOUNT_CONTROL, this.userAccountControl); Utils.SetProperty(newUser, User.PROPERTY_ACCOUNT_PWDLASTSET, -1); // 取消用户下次登陆时必须更改密码(默认为0) newUser.CommitChanges(); // reload this.Parse(newUser); newUser.Invoke("SetPassword", new object[] { newUserPassword }); // 在CommitChanges之后才能成功调用 newUser.CommitChanges(); } catch (DirectoryServicesCOMException dsce) { throw dsce; } finally { if (parent != null) { parent.Close(); parent.Dispose(); } if (newUser != null) { newUser.Close(); newUser.Dispose(); } } }