Beispiel #1
0
        private void btnGetUser_Click(object sender, RoutedEventArgs e)
        {
            if (!CheckInput())
            {
                SubDebug(string.Format("Input invalid."));
                return;
            }

            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));
            }
        }
Beispiel #2
0
        private OperationReturn CheckDomainInfo(SessionInfo session, List <string> listParams)
        {
            OperationReturn optReturn = new OperationReturn();

            optReturn.Result = true;
            optReturn.Code   = 0;
            try
            {
                //ListParam
                //0      域连接串串
                //1      域登录账号
                //2      域登录密码
                if (listParams == null || listParams.Count < 3)
                {
                    optReturn.Result  = false;
                    optReturn.Code    = Defines.RET_PARAM_INVALID;
                    optReturn.Message = string.Format("Request param is null or count invalid");
                    return(optReturn);
                }
                string    IStrADPath = listParams[0];
                string    username   = listParams[1];
                string    password   = listParams[2];
                ADUtility util       = new ADUtility(IStrADPath, username, password);

                ADUserCollection users = util.GetAllUsers();
                if (users == null)
                {
                    optReturn.Result  = false;
                    optReturn.Code    = Defines.RET_FAIL;
                    optReturn.Message = "Users null";
                    return(optReturn);
                }
                else
                {
                    optReturn.Result  = true;
                    optReturn.Code    = (int)S1112Codes.CheckDomainInfo;
                    optReturn.Message = "Check OK";
                    return(optReturn);
                }
            }
            catch (Exception ex)
            {
                optReturn.Result  = false;
                optReturn.Code    = Defines.RET_FAIL;
                optReturn.Message = ex.Message;
                return(optReturn);
            }
        }
        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));
            }
        }