/// <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="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> /// <returns></returns> public OU GetParent(string userName, string password) { DirectoryEntry de = null; try { de = ADManager.GetByGuid(this.Guid, userName, password); DirectoryEntry parentDe = null; try { parentDe = de.Parent; if (parent.SchemaClassName == SchemaClass.organizationalUnit.ToString("F")) { this.parent = new OU(parentDe); } else { this.parent = null; } } catch (DirectoryServicesCOMException dsce) { this.parent = null; throw dsce; } finally { if (parentDe != null) { parentDe.Close(); parentDe.Dispose(); } } } catch (DirectoryServicesCOMException dsce) { throw dsce; } finally { if (de != null) { de.Close(); de.Dispose(); } } return(this.parent); }
/// <summary> /// 获取组织单位子组织单位。 /// </summary> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> /// <returns></returns> public List <OU> GetChildren(string userName, string password) { this.children = new List <OU>(); OU ou = null; foreach (DirectoryEntry c in ADManager.Search(null, "organizationalUnit", null, this.Path, SearchScope.OneLevel, userName, password)) { ou = new OU(c); ou.parent = this; this.children.Add(ou); if (c != null) { c.Close(); c.Dispose(); } } return(this.children); }
private void GetSubTree(string parentPath, string userName, string password) { this.children = new List <OU>(); OU ou = null; foreach (DirectoryEntry c in ADManager.Search(null, "organizationalUnit", null, parentPath, SearchScope.OneLevel, userName, password)) { ou = new OU(c); ou.parent = this; this.children.Add(ou); ou.GetSubTree(c.Path, userName, password); if (c != null) { c.Close(); c.Dispose(); } } }