Beispiel #1
0
        public List <SearchResult> ExecuteSearch(DirectoryEntry root, string filter, ADSearchConditions condition, params string[] properties)
        {
            using (DirectorySearcher searcher = new DirectorySearcher(root))
            {
                searcher.Filter      = filter;
                searcher.SearchScope = condition.Scope;
                searcher.Sort        = condition.Sort;
                searcher.SizeLimit   = condition.SizeLimit;
                searcher.PageSize    = condition.PageSize;

                for (int i = 0; i < properties.Length; i++)
                {
                    searcher.PropertiesToLoad.Add(properties[i]);
                }

                List <SearchResult> result = searcher.FindAll().ToList();

                if (condition != null)
                {
                    condition.OnADSearchCompleted(searcher, result);
                }

                return(result);
            }
        }
Beispiel #2
0
        public void EnumGroupMembers(DirectoryEntry groupEntry, Action <SearchResult> action)
        {
            groupEntry.NullCheck("groupEntry");

            StringBuilder strB = new StringBuilder(256);

            foreach (string dn in groupEntry.Properties["member"])
            {
                strB.AppendFormat("(distinguishedName={0})", dn);
            }

            if (strB.Length > 0 && action != null)
            {
                ADSearchConditions condition = new ADSearchConditions();
                condition.Scope = SearchScope.Subtree;

                string filter = string.Format("(&(objectCategory=Person)(objectClass=person)(|{0}))", strB.ToString());
                using (DirectoryEntry entry = this.GetRootEntry())
                {
                    foreach (SearchResult sr in this.ExecuteSearch(entry, filter, condition, DefaultADObjProperties))
                    {
                        action(sr);
                    }
                }
            }
        }
Beispiel #3
0
 public ADSearchConditions(ADSearchConditions condition)
 {
     _Sort.Direction        = condition.Sort.Direction;
     _Sort.PropertyName     = condition.Sort.PropertyName;
     _Scope                 = condition.Scope;
     _SizeLimit             = condition.SizeLimit;
     SearchCompletedContext = condition.SearchCompletedContext;
 }
Beispiel #4
0
        public void ExecuteSearchRecursively(DirectoryEntry root, string filter, ADSearchConditions condition, ADSearchRecursivelyDelegate callback, object oParams, params string[] properties)
        {
            ExceptionHelper.FalseThrow <ArgumentNullException>(properties != null, "properties");

            if (Array.Exists(properties, data => data == "objectClass") == false)
            {
                properties = AppendToStringArray(properties, new string[] { "objectClass" });
            }

            InnerExecuteSearchRecursively(root, filter, condition, callback, oParams, null, properties);
        }
		public void ExecuteConvertion(AD2DBTransferContext transferContext)
		{
			ADSearchConditions condition = new ADSearchConditions(SearchScope.OneLevel);

			condition.ADSearchCompleted += new ADSearchCompletedHandler(condition_ADSearchCompleted);
			condition.SearchCompletedContext = transferContext;

			//condition.Sort = new SortOption("extensionName", SortDirection.Ascending);

			transferContext.InitialParams.DirectoryHelper.ExecuteSearchRecursively(transferContext.InitialParams.Root,
					ADSearchConditions.GetFilterByMask(ADSchemaType.Users | ADSchemaType.Organizations | ADSchemaType.Groups),
					condition,
					new ADSearchRecursivelyDelegate(EnumEntryItemCallBack),
					transferContext,
					AD2DBHelper.ADObjProperties);
		}
Beispiel #6
0
        /// <summary>
        /// 登录名称是否合法
        /// </summary>
        /// <param name="logonName">登录名称</param>
        /// <returns></returns>
        public bool IsLogOnNameValid(string logonName)
        {
            LogOnIdentity identity = new LogOnIdentity(logonName);

            ExceptionHelper.FalseThrow(string.Compare(identity.Domain, DomainShortName, true) == 0,
                                       string.Format("帐号“{0}”的域名与当前域“{1}”不匹配", identity.LogOnName, DomainShortName));

            using (DirectoryEntry root = GetRootEntry())
            {
                return(ExecuteSearch(root,
                                     ADSearchConditions.GetFilterByMask(ADSchemaType.Users,
                                                                        new ExtraFilter(string.Empty,
                                                                                        string.Format("(samAccountName={0})", EscapeString(identity.LogOnNameWithoutDomain)),
                                                                                        string.Empty)),
                                     new ADSearchConditions(SearchScope.Subtree), "samAccountName").Count > 0);
            }
        }
Beispiel #7
0
        public bool InnerExecuteSearchRecursively(DirectoryEntry root, string filter, ADSearchConditions condition, ADSearchRecursivelyDelegate callback, object oParams, object lastResult, string[] properties)
        {
            bool bContinue = true;

            ADSearchConditions newCondition = new ADSearchConditions(condition);

            newCondition.Scope = SearchScope.OneLevel;

            ADSearchResultParams asrp = new ADSearchResultParams(root);

            asrp.LastResult = lastResult;

            List <SearchResult> resultList = ExecuteSearch(root, filter, condition, properties);

            foreach (SearchResult sr in resultList)
            {
                if (bContinue)
                {
                    if (callback != null)
                    {
                        asrp.LastResult = callback(sr, asrp, oParams, ref bContinue);

                        if (bContinue == false)
                        {
                            break;
                        }
                    }

                    string objClass = sr.Properties["objectClass"][1].ToString();

                    if (string.Compare(objClass, "organizationalUnit", true) == 0)
                    {
                        bContinue = InnerExecuteSearchRecursively(sr.GetDirectoryEntry(), filter, condition, callback, oParams, asrp.LastResult, properties);
                    }
                }
            }

            return(bContinue);
        }
        public void Run()
        {
            FileInfo logfile = new FileInfo(Environment.CurrentDirectory.ToString() + "/log.txt");
            StreamWriter write = null;
            if (logfile.Exists)
            {
                write = logfile.AppendText();
                write.WriteLine("---------------------------------------------------------------------------------------");
            }
            else
            {
                write = logfile.CreateText();
            }

            write.WriteLine("用户名,导入结果,操作时间,失败原因");

            List<Picture> pictures = new List<Picture>();
            List<Picture> deletedPictures = new List<Picture>();
            Database database = null;
            try
            {
                #region 读取新加用户的图片的数据库信息
                database = DbHelper.GetDBDatabase("HB2008");
                string sql = @"select top 1000 c.LOGIN_NAME, a.FILE_NAME, a.CONTENT_DATA from WF.MATERIAL_CONTENT a join wf.IMAGE b " +
                              "on a.CONTENT_ID = b.ID join wf.INITIALIZATION_USER_AD_IMAGE c on b.RESOURCE_ID = c.PICTURE_ID where a.CLASS = 'HR_PHOTO' and c.UpdateStatus = 0 and c.VALIDSTATUS = 1 ";

                DbDataReader dr = database.ExecuteReader(CommandType.Text, sql);
                while (dr.Read())
                {
                    Picture pic = new Picture();

                    pic.LOGIN_NAME = dr["LOGIN_NAME"].ToString();
                    pic.FILE_NAME = dr["FILE_NAME"] != null ? dr["FILE_NAME"].ToString() : string.Empty;
                    pic.CONTENT_DATA = (byte[])dr["CONTENT_DATA"];
                    pictures.Add(pic);
                }
                dr.Close();
                #endregion

                #region 读取删除用户的图片的数据库信息
                sql = @"select LOGIN_NAME from WF.INITIALIZATION_USER_AD_IMAGE where VALIDSTATUS = 0 and UpdateStatus = 0  ";

                dr = database.ExecuteReader(CommandType.Text, sql);
                while (dr.Read())
                {
                    Picture pic = new Picture();

                    pic.LOGIN_NAME = dr["LOGIN_NAME"].ToString();
                    deletedPictures.Add(pic);
                }
                dr.Close();
                #endregion
            }
            catch
            {
                pictures.Clear();
                deletedPictures.Clear();
                write.WriteLine("SQL Server 服务器异常");
                return;
            }

            #region 向AD中写入图片信息
            foreach (Picture pic in pictures)
            {
                DirectoryEntry entity = null;
                List<SearchResult> result = null;
                try
                {
                    var exFilter = new ExtraFilter();
                    exFilter.UserFilter = "samAccountName=" + pic.LOGIN_NAME;
                    string filter = ADSearchConditions.GetFilterByMask(ADSchemaType.Users, exFilter);
                    ADSearchConditions conditons = new ADSearchConditions();
                    conditons.Scope = SearchScope.Subtree;
                    ServerInfo serverInfo = ServerInfoConfigSettings.GetConfig().ServerInfos["dc"].ToServerInfo();
                    MCS.Library.ADHelper adHelper = ADHelper.GetInstance(serverInfo);
                    result = adHelper.ExecuteSearch(adHelper.GetRootEntry(), filter, conditons, "distinguishedName");

					if (result.Count < 1)
                    {
                        write.WriteLine(pic.LOGIN_NAME + ",失败," + DateTime.Now + ",AD中找不到该账号");
                    }
                    else
                    {
                        #region 处理逻辑
                        entity = adHelper.NewEntry(result[0].Properties["distinguishedName"][0].ToString());
                        if (pic.CONTENT_DATA.Length < 102400)
                        {
                            if (entity.Properties.Contains("thumbnailPhoto"))
                            {
                                entity.Properties["thumbnailPhoto"][0] = pic.CONTENT_DATA;
                            }
                            else
                            {
                                entity.Properties["thumbnailPhoto"].Add(pic.CONTENT_DATA);
                            }
                        }
                        else
                        {
                            ImageHelper imgHelp = new ImageHelper(pic.CONTENT_DATA);
                            double percent = 80000.00 / pic.CONTENT_DATA.Length;

                            if (pic.FILE_NAME != string.Empty && pic.FILE_NAME != null)
                            {
                                if (pic.FILE_NAME.Split('.').Length > 1)
                                {
                                    if (pic.FILE_NAME.Split('.')[1].ToLower() == "jpg" || pic.FILE_NAME.Split('.')[1].ToLower() == "gif" || pic.FILE_NAME.Split('.')[1].ToLower() == "png" || pic.FILE_NAME.Split('.')[1].ToLower() == "bmp")
                                    {

                                        if (entity.Properties.Contains("thumbnailPhoto"))
                                        {
                                            entity.Properties["thumbnailPhoto"][0] = imgHelp.GetThumbnailImage(pic.FILE_NAME.Split('.')[1].ToLower(), percent);
                                        }
                                        else
                                        {
                                            entity.Properties["thumbnailPhoto"].Add(imgHelp.GetThumbnailImage(pic.FILE_NAME.Split('.')[1].ToLower(), percent));
                                        }
                                    }
                                    else
                                    {
                                        if (entity.Properties.Contains("thumbnailPhoto"))
                                        {
                                            entity.Properties["thumbnailPhoto"][0] = imgHelp.GetThumbnailImage("jpg", percent);
                                        }
                                        else
                                        {
                                            entity.Properties["thumbnailPhoto"].Add(imgHelp.GetThumbnailImage("jpg", percent));
                                        }
                                    }
                                }
                                else
                                {
                                    if (entity.Properties.Contains("thumbnailPhoto"))
                                    {
                                        entity.Properties["thumbnailPhoto"][0] = imgHelp.GetThumbnailImage("jpg", percent);
                                    }
                                    else
                                    {
                                        entity.Properties["thumbnailPhoto"].Add(imgHelp.GetThumbnailImage("jpg", percent));
                                    }
                                }
                            }
                            else
                            {
                                if (entity.Properties.Contains("thumbnailPhoto"))
                                {
                                    entity.Properties["thumbnailPhoto"][0] = imgHelp.GetThumbnailImage("jpg", percent);
                                }
                                else
                                {
                                    entity.Properties["thumbnailPhoto"].Add(imgHelp.GetThumbnailImage("jpg", percent));
                                }
                            }
                        }
                        #endregion
                        entity.CommitChanges();
                        entity.Close();
                    
                        string sql = @"update WF.INITIALIZATION_USER_AD_IMAGE set UPDATESTATUS = 1 , UPDATETIME = GETDATE() where LOGIN_NAME = '" + pic.LOGIN_NAME + "' ";
                        database.ExecuteNonQuery(CommandType.Text, sql);
                        write.WriteLine(pic.LOGIN_NAME + ",成功," + DateTime.Now + ",");
                    }
                }
                catch (Exception ex)
                {
                    if (entity != null)
                    {
                        entity.Close();
                    }

                    write.WriteLine(pic.LOGIN_NAME + ",失败," + DateTime.Now + "," + ex.Message);
                }
            }

            foreach (Picture pic in deletedPictures)
            {
                DirectoryEntry entity = null;
                List<SearchResult> result = null;
                try
                {
                    var exFilter = new ExtraFilter();
                    exFilter.UserFilter = "samAccountName=" + pic.LOGIN_NAME;
                    string filter = ADSearchConditions.GetFilterByMask(ADSchemaType.Users, exFilter);
                    ADSearchConditions conditons = new ADSearchConditions();
                    conditons.Scope = SearchScope.Subtree;
                    ServerInfo serverInfo = ServerInfoConfigSettings.GetConfig().ServerInfos["dc"].ToServerInfo();
                    MCS.Library.ADHelper adHelper = ADHelper.GetInstance(serverInfo);
                    result = adHelper.ExecuteSearch(adHelper.GetRootEntry(), filter, conditons, "distinguishedName");
                    if (result.Count < 1)
                    {
                        write.WriteLine(pic.LOGIN_NAME + ",失败," + DateTime.Now + ",AD中找不到该账号");
                    }
                    else
                    {
                        entity = adHelper.NewEntry(result[0].Properties["distinguishedName"][0].ToString());

                        if (entity.Properties.Contains("thumbnailPhoto"))
                        {
                            entity.Properties["thumbnailPhoto"].Clear();
                        }
                        
                        entity.CommitChanges();
                        entity.Close();

                        string sql = @"update WF.INITIALIZATION_USER_AD_IMAGE set UPDATESTATUS = 1, UPDATETIME = GETDATE() where LOGIN_NAME = '" + pic.LOGIN_NAME + "' ";
                        database.ExecuteNonQuery(CommandType.Text, sql);
                        write.WriteLine(pic.LOGIN_NAME + ",成功," + DateTime.Now + ",");
                    }
                }
                catch (Exception ex)
                {
                    if (entity != null)
                    {
                        entity.Close();
                    }

                    write.WriteLine(pic.LOGIN_NAME + ",失败," + DateTime.Now + "," + ex.Message);
                }
            }

            write.Close();
            #endregion

            pictures.Clear();
            deletedPictures.Clear();
        }
		private void treeView_AfterExpand(object sender, TreeViewEventArgs e)
		{
			if (e.Action == TreeViewAction.Expand)
			{
				if (e.Node.Nodes.Count > 0 && e.Node.Nodes[0].Tag != null && e.Node.Nodes[0].Tag.ToString() == "Fake")
				{
					if (e.Node.Tag != null)
					{
						ADHelper helper = GetADHelper();

						ADSearchConditions conditions = new ADSearchConditions(SearchScope.OneLevel);

						using (DirectoryEntry entry = helper.NewEntry(e.Node.Tag.ToString()))
						{
							e.Node.Nodes.Clear();

							foreach (SearchResult sr in helper.ExecuteSearch(entry,
								ADSearchConditions.GetFilterByMask(ADSchemaType.Groups | ADSchemaType.Users | ADSchemaType.Organizations), conditions))
							{
								BindSearchResult(sr, e.Node);
							}
						}
					}
				}
			}
		}
Beispiel #10
0
        public bool InnerExecuteSearchRecursively(DirectoryEntry root, string filter, ADSearchConditions condition, ADSearchRecursivelyDelegate callback, object oParams, object lastResult, string[] properties)
        {
            bool bContinue = true;

            ADSearchConditions newCondition = new ADSearchConditions(condition);

            newCondition.Scope = SearchScope.OneLevel;

            ADSearchResultParams asrp = new ADSearchResultParams(root);

            asrp.LastResult = lastResult;

            List<SearchResult> resultList = ExecuteSearch(root, filter, condition, properties);

            foreach (SearchResult sr in resultList)
            {
                if (bContinue)
                {
                    if (callback != null)
                    {
                        asrp.LastResult = callback(sr, asrp, oParams, ref bContinue);

                        if (bContinue == false)
                            break;
                    }

                    string objClass = sr.Properties["objectClass"][1].ToString();

                    if (string.Compare(objClass, "organizationalUnit", true) == 0)
                    {
                        bContinue = InnerExecuteSearchRecursively(sr.GetDirectoryEntry(), filter, condition, callback, oParams, asrp.LastResult, properties);
                    }
                }
            }

            return bContinue;
        }
Beispiel #11
0
        public void ExecuteSearchRecursively(DirectoryEntry root, string filter, ADSearchConditions condition, ADSearchRecursivelyDelegate callback, object oParams, params string[] properties)
        {
            ExceptionHelper.FalseThrow<ArgumentNullException>(properties != null, "properties");

            if (Array.Exists(properties, data => data == "objectClass") == false)
                properties = AppendToStringArray(properties, new string[] { "objectClass" });

            InnerExecuteSearchRecursively(root, filter, condition, callback, oParams, null, properties);
        }
Beispiel #12
0
        public List<SearchResult> ExecuteSearch(DirectoryEntry root, string filter, ADSearchConditions condition, params string[] properties)
        {
            using (DirectorySearcher searcher = new DirectorySearcher(root))
            {
                searcher.Filter = filter;
                searcher.SearchScope = condition.Scope;
                searcher.Sort = condition.Sort;
                searcher.SizeLimit = condition.SizeLimit;
                searcher.PageSize = condition.PageSize;

                for (int i = 0; i < properties.Length; i++)
                {
                    searcher.PropertiesToLoad.Add(properties[i]);
                }

                List<SearchResult> result = searcher.FindAll().ToList();

                if (condition != null)
                    condition.OnADSearchCompleted(searcher, result);

                return result;
            }
        }
Beispiel #13
0
        public List<SearchResult> ExecuteSearch(DirectoryEntry root, string filter, params string[] properties)
        {
            using (DirectorySearcher searcher = new DirectorySearcher(root))
            {
                ADSearchConditions condition = new ADSearchConditions();

                searcher.Filter = filter;
                searcher.SearchScope = condition.Scope;
                searcher.Sort = condition.Sort;
                searcher.SizeLimit = condition.SizeLimit;
                searcher.PageSize = condition.PageSize;

                for (int i = 0; i < properties.Length; i++)
                {
                    searcher.PropertiesToLoad.Add(properties[i]);
                }

                return searcher.FindAll().ToList();
            }
        }
Beispiel #14
0
        public void EnumGroupMembers(DirectoryEntry groupEntry, Action<SearchResult> action)
        {
            groupEntry.NullCheck("groupEntry");

            StringBuilder strB = new StringBuilder(256);

            foreach (string dn in groupEntry.Properties["member"])
            {
                strB.AppendFormat("(distinguishedName={0})", dn);
            }

            if (strB.Length > 0 && action != null)
            {
                ADSearchConditions condition = new ADSearchConditions();
                condition.Scope = SearchScope.Subtree;

                string filter = string.Format("(&(objectCategory=Person)(objectClass=person)(|{0}))", strB.ToString());
                using (DirectoryEntry entry = this.GetRootEntry())
                {
                    foreach (SearchResult sr in this.ExecuteSearch(entry, filter, condition, DefaultADObjProperties))
                    {
                        action(sr);
                    }
                }
            }
        }
Beispiel #15
0
 public ADSearchConditions(ADSearchConditions condition)
 {
     _Sort.Direction = condition.Sort.Direction;
     _Sort.PropertyName = condition.Sort.PropertyName;
     _Scope = condition.Scope;
     _SizeLimit = condition.SizeLimit;
     SearchCompletedContext = condition.SearchCompletedContext;
 }