Esempio n. 1
0
        /// <summary>
        ///  在根目录下创建组织单元
        /// </summary>
        /// <param name="ou">组织单元名称</param>
        /// <returns></returns>
        public bool CreateNewUnit(string ou, string ouName)
        {
            bool result = false;

            ///获得创建DirectoryEntry 的父对象
            string         LDAPDomain = "/" + Iadc.GetLDAPDomain();
            DirectoryEntry oDE        = Iadc.GetDirectoryObject(LDAPDomain);
            DirectoryEntry oDEC       = new DirectoryEntry();

            //校验是否存在全局同名的组织单元
            if (!Iadch.CheckUnit(ou))
            {
                try
                {
                    //创建子组织单元
                    oDEC = oDE.Children.Add("OU=" + ou, "organizationalunit");
                    oDEC.Properties["description"].Value = ouName;
                    oDEC.Properties["l"].Value           = "1";
                    oDEC.CommitChanges();
                    result = true;
                }
                catch (Exception err)
                {
                    result = false;
                }
            }

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 将用户移动到容器
        /// </summary>
        /// <param name="cn">UserName</param>
        /// <param name="ou">容器</param>
        public int MoveUserToUnit(string cn, string ou, string father_OU, out string errStr)
        {
            int result = 0;

            errStr = "";
            string LDAPDomain = "/" + father_OU.ToString() + Iadc.GetLDAPDomain();

            DirectoryEntry oUnit = new DirectoryEntry();
            DirectoryEntry oUser = new DirectoryEntry();

            if (!Iadch.CheckUnit(ou))
            {
                errStr = "未找到指定的机构/部门";
                return(3);
            }

            if (!Iadch.CheckUser(cn))
            {
                errStr = "未找到指定的用户";
                return(2);
            }

            try
            {
                oUnit = Iads.GetUnitEntry(ou, LDAPDomain);
                oUser = Iads.GetUserEntry(cn);
                if (!oUnit.Properties["member"].Contains(oUser.Properties["distinguishedName"].Value))
                {
                    oUser.Properties["l"].Value = "4";

                    oUser.MoveTo(oUnit);
                    oUser.CommitChanges();
                    oUnit.CommitChanges();
                    result = 1;
                }
            }
            catch (Exception err)
            {
                result = 0;
            }
            finally
            {
                oUser.Close();
            }
            return(result);
        }