Exemplo n.º 1
0
        /// <summary>
        /// 获取OU下的Users
        /// </summary>
        /// <param name="ldapPath"></param>
        /// <returns></returns>
        public static List <AdModel> GetOUUsersByPath(string ldapPath)
        {
            List <AdModel> list = new List <AdModel>();

            DirectoryEntry searchRoot = new DirectoryEntry(ldapPath, ADUser, ADPassword, AuthenticationTypes.Secure);

            foreach (DirectoryEntry subEntry in searchRoot.Children)
            {
                string entrySchemaClsName = subEntry.SchemaClassName;

                string[] arr         = subEntry.Name.Split('=');
                string   categoryStr = arr[0];
                string   nameStr     = arr[1];
                string   id          = Convert.ToString(subEntry.Guid);

                bool isExist = list.Exists(d => d.Id == id);

                if (entrySchemaClsName == "user")
                {
                    //查看当前用户是否已经禁用
                    bool isDisable = Convert.ToBoolean(Convert.ToInt32(subEntry.Properties["userAccountcontrol"][0]) & 0x0002);

                    if (isDisable)
                    {
                        continue;
                    }

                    string accountName = string.Empty;
                    string displayName = string.Empty;

                    if (subEntry.Properties.Contains("samaccountName"))
                    {
                        accountName = subEntry.Properties["samaccountName"][0].ToString();
                    }
                    if (subEntry.Properties.Contains("displayName"))
                    {
                        displayName = subEntry.Properties["displayName"][0].ToString();
                    }

                    if (!isExist)
                    {
                        AdModel adModel = new AdModel();
                        adModel.Id        = id;
                        adModel.LDAPPath  = "";
                        adModel.LoginName = accountName;
                        adModel.Name      = displayName;
                        adModel.TypeId    = (int)TypeEnum.USER;
                        adModel.ParentId  = Convert.ToString(searchRoot.Guid);

                        list.Add(adModel);
                    }
                }
            }

            return(list);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 同步制定OU路径下的所有子OU和Users
        /// </summary>
        /// <param name="adLDAPPath">OULdAp路径</param>
        public static List <AdModel> GetFirstLevelOUByPath(string[] adLDAPPath)
        {
            List <AdModel> listadMOdel = new List <AdModel>();

            foreach (string path in adLDAPPath)
            {
                string LDAPPath = path + "," + LDAPDomain;

                DirectoryEntry searchRoot = new DirectoryEntry(LDAPPath, ADUser, ADPassword, AuthenticationTypes.Secure);

                if (searchRoot.Properties.Contains("ou"))
                {
                    string rootOuName = searchRoot.Properties["ou"][0].ToString();

                    string id = Convert.ToString(searchRoot.Guid);

                    string  ldapPath = searchRoot.Path;
                    AdModel adMOdel  = new AdModel();
                    adMOdel.Id        = id;
                    adMOdel.LDAPPath  = ldapPath;
                    adMOdel.LoginName = rootOuName;
                    adMOdel.Name      = rootOuName;
                    adMOdel.TypeId    = (int)TypeEnum.OU;
                    adMOdel.ParentId  = "0";

                    listadMOdel.Add(adMOdel);

                    logger.Info("Ldap地址:" + LDAPPath + "获取到第一层OU信息: " + rootOuName + " 用户:" + ADUser);
                }
                else
                {
                    logger.Info("Ldap地址:" + LDAPPath + "未获取到第一层OU信息 " + " 用户:" + ADUser);
                }
            }

            return(listadMOdel);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取第一层OU下的子OU
        /// </summary>
        /// <param name="ldapPath"></param>
        /// <returns></returns>
        public static List <AdModel> GetSubUnitsByPath(string ldapPath)
        {
            List <AdModel> list = new List <AdModel>();

            DirectoryEntry searchRoot = new DirectoryEntry(ldapPath, ADUser, ADPassword, AuthenticationTypes.Secure);

            foreach (DirectoryEntry subEntry in searchRoot.Children)
            {
                string entrySchemaClsName = subEntry.SchemaClassName;

                string[] arr         = subEntry.Name.Split('=');
                string   categoryStr = arr[0];
                string   nameStr     = arr[1];
                string   id          = Convert.ToString(subEntry.Guid);
                string   subLdapPath = subEntry.Path;

                bool isExist = list.Exists(d => d.Id == id);

                if (entrySchemaClsName == "organizationalUnit")
                {
                    if (!isExist)
                    {
                        AdModel adModel = new AdModel();
                        adModel.Id        = id;
                        adModel.LDAPPath  = subLdapPath;
                        adModel.LoginName = nameStr;
                        adModel.Name      = nameStr;
                        adModel.TypeId    = (int)TypeEnum.OU;
                        adModel.ParentId  = Convert.ToString(searchRoot.Guid);
                        list.Add(adModel);
                    }
                }
            }

            return(list);
        }
Exemplo n.º 4
0
        public static void GetADInfoByPath(string path, List <AdModel> list, string parentName)
        {
            DirectoryEntry searchRoot = new DirectoryEntry(path, ADUser, ADPassword, AuthenticationTypes.Secure);

            //将该层的子集添加到list集合
            foreach (DirectoryEntry subEntry in searchRoot.Children)
            {
                string entrySchemaClsName = subEntry.SchemaClassName;

                string[] arr             = subEntry.Name.Split('=');
                string   categoryStr     = arr[0];
                string   nameStr         = arr[1];
                string   id              = Convert.ToString(subEntry.Guid);
                string   subLdapPath     = subEntry.Path;
                string   parentGroupName = string.Empty;

                bool isChildExist = list.Exists(d => d.Id == id);

                switch (entrySchemaClsName)
                {
                case "user":
                    //查看当前用户是否已经禁用
                    bool isDisable = Convert.ToBoolean(Convert.ToInt32(subEntry.Properties["userAccountcontrol"][0]) & 0x0002);

                    if (isDisable)
                    {
                        continue;
                    }

                    string accountName = string.Empty;
                    string displayName = string.Empty;

                    if (subEntry.Properties.Contains("samaccountName"))
                    {
                        accountName = subEntry.Properties["samaccountName"][0].ToString();
                    }
                    if (subEntry.Properties.Contains("displayName"))
                    {
                        displayName = subEntry.Properties["displayName"][0].ToString();
                    }

                    if (!isChildExist)
                    {
                        AdModel adModel = new AdModel();
                        adModel.Id        = id;
                        adModel.LDAPPath  = "";
                        adModel.LoginName = accountName;
                        adModel.Name      = displayName;
                        adModel.TypeId    = (int)TypeEnum.USER;
                        adModel.ParentId  = Convert.ToString(searchRoot.Guid);

                        list.Add(adModel);
                    }
                    break;

                case "organizationalUnit":
                    if (!isChildExist)
                    {
                        parentGroupName = parentName + "_" + nameStr;

                        AdModel adModel = new AdModel();
                        adModel.Id        = id;
                        adModel.LDAPPath  = subLdapPath;
                        adModel.LoginName = nameStr;
                        adModel.Name      = parentGroupName;
                        adModel.TypeId    = (int)TypeEnum.OU;
                        adModel.ParentId  = Convert.ToString(searchRoot.Guid);

                        list.Add(adModel);
                    }

                    GetADInfoByPath(subEntry.Path, list, parentGroupName);
                    break;
                }
            }
        }