示例#1
0
        private void InitDomainTree()
        {
            if (domainInfo == null)
            {
                return;
            }
            IStrADDomain   = domainInfo.DomainName;
            IStrADPassword = domainInfo.DomainUserPassWord;
            IStrADUser     = domainInfo.DomainUserName;
            if (IStrADDomain == string.Empty || IStrADPassword == string.Empty || IStrADUser == string.Empty)
            {
                return;
            }
            string LDAPInfo = string.Format("{0}:{1}({2}:{3})", CurrentApp.GetLanguageInfo("11011602", "域账号"), IStrADUser, CurrentApp.GetLanguageInfo("11011601", "域名"), IStrADDomain);

            IListADUsers.Clear();
            List <BindItem> listItems = new List <BindItem>();

            IStrADPath = string.Format("LDAP://{0}", IStrADDomain);
            util       = new ADUtility(IStrADPath, IStrADUser, IStrADPassword);
            ClearChildren(mRoot);       //树中会清理掉list里面的部分内容
            mListDomainObjItem.Clear(); //清理剩余的内容
            try
            {
                //获取下面所有的OU
                ADGroupCollection OUs = util.GetAllOrganizationalUnit();
                foreach (ADGroup group in OUs.AllItem)
                {
                    DomainObjectItem doi = new DomainObjectItem();
                    doi.Name      = group.Name;
                    doi.FullName  = group.Name;
                    doi.mGuid     = group.Guid;
                    doi.ObjType   = 111;
                    doi.IsChecked = false;
                    doi.Icon      = "Images/org.ico";

                    DirectoryEntry de = group.MyDirectoryEntry;
                    doi.ParentGuid     = de.Parent.Guid;
                    doi.ParentName     = de.Parent.Name.Substring(3);
                    doi.ParentFullName = doi.ParentName;
                    mListDomainObjItem.Add(doi);
                }
                //获取下面所有用户
                ADUserCollection Users = util.GetAllUsers();
                foreach (ADUser user in Users)
                {
                    //判断用户是否禁用
                    int    ControlCode     = user.UserAccountControl;
                    string UserControlCode = Convert.ToString(ControlCode, 2);
                    UserControlCode = UserControlCode.Substring(UserControlCode.Count() - 2, 1);
                    if (UserControlCode == "1")
                    {
                        continue;
                    }

                    DomainObjectItem doi = new DomainObjectItem();
                    doi.Name     = user.Name.Substring(3).ToLower();
                    doi.FullName = user.AccountName;
                    if (doi.FullName == string.Empty)
                    {
                        doi.FullName = string.Format("{0}@{1}", IStrADDomain.ToLower(), user.AccountFullName.ToLower());
                    }
                    else
                    {
                        List <string> listsp = doi.FullName.Split('@').ToList();
                        if (listsp.Count == 2)
                        {
                            doi.FullName = string.Format("{0}@{1}", IStrADDomain.ToLower(), listsp[0].ToLower());
                        }
                    }
                    doi.mGuid     = user.Guid;
                    doi.ObjType   = 112;
                    doi.IsChecked = false;
                    doi.Icon      = "Images/user.ico";

                    DirectoryEntry de = user.MyDirectoryEntry;
                    doi.ParentGuid     = de.Parent.Guid;
                    doi.ParentName     = de.Parent.Name.Substring(3);
                    doi.ParentFullName = doi.ParentName;
                    mListDomainObjItem.Add(doi);
                }
                //======================================================================
                //获取组织结构distinguishedName
                ADUser Duser = util.GetADUser(IStrADUser);
                //ADGroupCollection groups = Duser.MemberOf;
                object obj          = Duser.GetProperty("distinguishedName");
                string OUCollection = obj.ToString();
                //拆分string,获取dc下的第一个机构
                List <string> OUsName = OUCollection.Split(',').ToList();
                int           count = 0;
                string        OUName = string.Empty; string DName = OUsName[OUsName.Count - 2];
                for (; count < OUsName.Count(); count++)
                {
                    string tempOU = OUsName[count];
                    if (tempOU.Substring(0, 2) == "OU")
                    {
                        OUName = tempOU.Substring(3);
                        DomainObjectItem objItem = new DomainObjectItem();
                        objItem.Name      = OUName;
                        objItem.FullName  = objItem.Name;
                        objItem.ObjType   = 111;
                        objItem.IsChecked = false;
                        objItem.Icon      = "Images/org.ico";
                        mRoot.AddChild(objItem);
                        mListDomainObjItem.Add(objItem);
                        GetChild(objItem);
                        break;
                    }
                }
                if (OUName == string.Empty && count == OUsName.Count)//是域下面的用户,直接获取整个结构树
                {
                    mRoot.Name = DName.Substring(3);
                    GetChild(mRoot);
                }
            }
            catch (Exception ex)
            {
                ShowException(string.Format("Get Users Info From LDAP Fail:{0}", ex.Message));
                CurrentApp.WriteLog(string.Format("Get all AD users fail.\t{0}", ex.Message));
            }
        }
示例#2
0
        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            if (!CheckInput())
            {
                SubDebug(string.Format("Input invalid."));
                return;
            }

            string strMsg = string.Empty;

            IStrADPath = string.Format("LDAP://{0}", IStrADDomain);
            ADUtility util = new ADUtility(IStrADPath, IStrADUser, IStrADPassword);

            try
            {
                ADUser adUser = util.GetADUser(IStrADUser);;
                SubDebug(string.Format("{0}", adUser.AccountName));
            }
            catch (Exception ex)
            {
                SubDebug(string.Format("{0}", ex.Message));
            }

            //InitADGroupData();

            //IListADUsers.Clear();
            //IStrADPath = string.Format("LDAP://{0}", IStrADDomain);
            //ADUtility util = new ADUtility(IStrADPath, IStrADUser, IStrADPassword);
            //try
            //{
            //    ADUserCollection users = util.GetAllUsers();
            //    foreach (ADUser user in users)
            //    {
            //        BindItem item = new BindItem();
            //        item.Name = user.AccountName;
            //        item.Display = user.DisplayName;
            //        item.Obj = item;
            //        IListADUsers.Add(item);
            //    }
            //}
            //catch (Exception ex)
            //{
            //    SubDebug(string.Format("Get all AD users fail.\t{0}", ex.Message));
            //}

            //IListADUsers.Clear();
            //IStrADPath = string.Format("LDAP://{0}", IStrADDomain);
            //string strFilter = "(&(objectclass=user)(!objectclass=computer)(cn=*))";
            //DirectoryEntry de = new DirectoryEntry(IStrADPath, IStrADUser, IStrADPassword);
            //DirectorySearcher searcher = new DirectorySearcher(de);
            //searcher.Filter = strFilter;
            //searcher.SearchScope = SearchScope.Subtree;
            //try
            //{
            //    SearchResultCollection items = searcher.FindAll();
            //    foreach (SearchResult result in items)
            //    {
            //        DirectoryEntry deItem = result.GetDirectoryEntry();
            //        string name = string.Empty;
            //        string display = string.Empty;
            //        try
            //        {
            //            name = deItem.Properties["SAMAccountName"].Value.ToString();
            //        }
            //        catch (Exception ex)
            //        {
            //            SubDebug(string.Format("Get Name property value fail.\t{0}", ex.Message));
            //        }
            //        try
            //        {
            //            display = deItem.Properties["displayName"].Value.ToString();
            //        }
            //        catch (Exception ex)
            //        {
            //            SubDebug(string.Format("Get display property value fail.\t{0}", ex.Message));
            //        }
            //        BindItem item = new BindItem();
            //        item.Name = name;
            //        item.Display = display;
            //        item.Obj = item;
            //        IListADUsers.Add(item);
            //    }
            //}
            //catch (Exception ex)
            //{
            //    SubDebug(string.Format("Get all ad users fail.\t{0}", ex.Message));
            //}
        }