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); } } } } } }
private static IEnumerable <SearchResult> GetSearchResultsByPropertyValues(ADHelper adHelper, string propertyName, IEnumerable propertyValues, string[] properties, int sizeLimit, string filter) { ADSearchConditions condition = new ADSearchConditions(SearchScope.Subtree); condition.SizeLimit = sizeLimit; condition.PageSize = sizeLimit; propertyName = ADHelper.EscapeString(propertyName); StringBuilder postfixBuilder = new StringBuilder(); foreach (var val in propertyValues) { postfixBuilder.Append("(").Append(propertyName).Append("=").Append(ADHelper.EscapeValueForLdapQuery(val)).Append(")"); } if (postfixBuilder.Length > 0) { var postExpression = postfixBuilder.Length > 0 ? "(|" + postfixBuilder.ToString() + ")" : string.Empty; filter = string.IsNullOrEmpty(filter) ? postExpression : "(&" + filter + postExpression + ")"; using (DirectoryEntry parentEntry = adHelper.GetRootEntry()) { var searchList = adHelper.ExecuteSearch(parentEntry, filter, condition, properties); return(searchList); } } else { return(new SearchResult[0]); } }
public static IEnumerable <SearchResult> GetSearchResultsByDNList(ADHelper adHelper, IEnumerable <string> rdnList, ADSchemaType schemaType, string[] properties, int sizeLimit) { ADSearchConditions condition = new ADSearchConditions(SearchScope.Subtree); condition.SizeLimit = sizeLimit; condition.PageSize = sizeLimit; var filter = ADSearchConditions.GetFilterByMask(schemaType); StringBuilder postfixBuilder = new StringBuilder(); foreach (var val in rdnList) { postfixBuilder.Append("(").Append("distinguishedName=").Append(AppendNamingContext(val)).Append(")"); } if (postfixBuilder.Length > 0) { var postExpression = postfixBuilder.Length > 0 ? "(|" + postfixBuilder.ToString() + ")" : string.Empty; filter = string.IsNullOrEmpty(filter) ? postExpression : "(&" + filter + postExpression + ")"; using (DirectoryEntry parentEntry = adHelper.GetRootEntry()) { var searchList = adHelper.ExecuteSearch(parentEntry, filter, condition, properties); return(searchList); } } else { return(new SearchResult[0]); } }
public static SearchResult GetSearchResultByDN(ADHelper adHelper, string DN, ADSchemaType schemaType) { ADSearchConditions condition = new ADSearchConditions(SearchScope.Subtree); condition.SizeLimit = 1; string filter = string.Empty; switch (schemaType) { case ADSchemaType.Organizations: filter = "(objectCategory=organizationalUnit)"; break; case ADSchemaType.Users: filter = "(&(objectCategory=person)(objectClass=user))"; break; case ADSchemaType.Groups: filter = "(&(objectCategory=group)(objectClass=group))"; break; } filter = string.Format("(&(distinguishedName={0}){1})", AppendNamingContext(DN), filter); using (DirectoryEntry parentEntry = adHelper.GetRootEntry()) { var searchList = adHelper.ExecuteSearch(parentEntry, filter, condition, ADObjNeededProperties); return(searchList.FirstOrDefault()); } }
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); }
public static SearchResult GetSearchResultByID(ADHelper adHelper, string nativeID, string[] neededProperties) { ADSearchConditions condition = new ADSearchConditions(SearchScope.Subtree); condition.SizeLimit = 1; string filter = string.Empty; filter = string.Format("(objectGuid={0})", ConvertGuidToOctectString(nativeID)); using (DirectoryEntry parentEntry = adHelper.GetRootEntry()) { var searchList = adHelper.ExecuteSearch(parentEntry, filter, condition, neededProperties); return(searchList.FirstOrDefault()); } }
/// <summary> /// /// </summary> /// <param name="strUserID"></param> /// <returns></returns> protected override DirectoryEntry GetUserDirectoryEntry(string strUserID) { ServerInfo si = GetServerInfo(); ADHelper helper = ADHelper.GetInstance(si); using (DirectoryEntry root = helper.GetRootEntry()) { string filter = ADSearchConditions.GetFilterByMask(ADSchemaType.Users, new ExtraFilter(string.Empty, "(samAccountName=" + strUserID + ")", string.Empty)); ADSearchConditions condition = new ADSearchConditions(SearchScope.Subtree); List<SearchResult> result = helper.ExecuteSearch(root, filter, condition, "distinguishedName"); ExceptionHelper.FalseThrow(result.Count > 0, "不能在域上找到登录名为{0}的用户", strUserID); return helper.NewEntry(helper.GetSearchResultPropertyStrValue("distinguishedName", result[0])); } }
/// <summary> /// /// </summary> /// <param name="strUserID"></param> /// <returns></returns> protected override DirectoryEntry GetUserDirectoryEntry(string strUserID) { ServerInfo si = GetServerInfo(); ADHelper helper = ADHelper.GetInstance(si); using (DirectoryEntry root = helper.GetRootEntry()) { string filter = ADSearchConditions.GetFilterByMask(ADSchemaType.Users, new ExtraFilter(string.Empty, "(samAccountName=" + strUserID + ")", string.Empty)); ADSearchConditions condition = new ADSearchConditions(SearchScope.Subtree); List <SearchResult> result = helper.ExecuteSearch(root, filter, condition, "distinguishedName"); ExceptionHelper.FalseThrow(result.Count > 0, "不能在域上找到登录名为{0}的用户", strUserID); return(helper.NewEntry(helper.GetSearchResultPropertyStrValue("distinguishedName", result[0]))); } }
public static List <ADObjectWrapper> FindAllChildrenUser(this DirectoryEntry entry) { List <ADObjectWrapper> result = new List <ADObjectWrapper>(); ADHelper adHelper = SynchronizeContext.Current.ADHelper; ADSearchConditions condition = new ADSearchConditions(SearchScope.Subtree); condition.PageSize = 1000; List <SearchResult> searchList = adHelper.ExecuteSearch(entry, ADSearchConditions.GetFilterByMask(ADSchemaType.Users), condition, ADUserObjNeededProperties); foreach (SearchResult searchResult in searchList) { result.Add(searchResult.ToADOjectWrapper()); } return(result); }
public static List <ADObjectWrapper> SearchChildren(ADObjectWrapper adObject) { List <ADObjectWrapper> result = new List <ADObjectWrapper>(); ADHelper adHelper = SynchronizeContext.Current.ADHelper; ADSearchConditions condition = new ADSearchConditions(SearchScope.OneLevel); using (DirectoryEntry parentEntry = adHelper.NewEntry(adObject.DN)) { List <SearchResult> searchList = adHelper.ExecuteSearch(parentEntry, ADSearchConditions.GetFilterByMask(ADSchemaType.Users | ADSchemaType.Organizations | ADSchemaType.Groups), condition, ADObjNeededProperties); foreach (var searchResult in searchList) { result.Add(searchResult.ToADOjectWrapper()); } } return(result); }
public static SearchResult GetSearchResultByLogonName(string logonName, ADSchemaType schemaType) { ADHelper adHelper = SynchronizeContext.Current.ADHelper; ADSearchConditions condition = new ADSearchConditions(SearchScope.Subtree); condition.SizeLimit = 1; string filter = string.Empty; string[] neededProperties = GetNeededProperties(schemaType); filter = string.Format("(samAccountName={0})", ADHelper.EscapeValueForLdapQuery(logonName)); using (DirectoryEntry parentEntry = adHelper.GetRootEntry()) { var searchList = adHelper.ExecuteSearch(parentEntry, filter, condition, neededProperties); return(searchList.FirstOrDefault()); } }
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 { lblResult.Text = "失败"; 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(); } lblResult.Text = "失败"; 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(); } lblResult.Text = "失败"; write.WriteLine(pic.LOGIN_NAME + ",失败," + DateTime.Now + "," + ex.Message); } } write.Close(); #endregion pictures.Clear(); deletedPictures.Clear(); if (lblResult.Text == "失败") { lblResult.Text = "失败,请查看日志记录"; } else { lblResult.Text = "成功"; } }
/// <summary> /// 基于单个属性的精确匹配多个结果 /// </summary> /// <param name="adHelper"></param> /// <param name="propertyName">匹配的单一属性名称</param> /// <param name="propertyValues">属性值,注意如果是字符串,会替换其中的特殊字符,只能使用字节数组或基元类型。</param> /// <param name="schemaType"></param> /// <param name="properties">要提取的属性名</param> /// <param name="sizeLimit">返回结果的数量</param> /// <returns></returns> public static IEnumerable <SearchResult> GetSearchResultsByPropertyValues(ADHelper adHelper, string propertyName, IEnumerable propertyValues, ADSchemaType schemaType, string[] properties, int sizeLimit) { var filter = ADSearchConditions.GetFilterByMask(schemaType); return(GetSearchResultsByPropertyValues(adHelper, propertyName, propertyValues, properties, sizeLimit, filter)); }