C#读取ActiveDirectory(曹海涛)

网友投稿 654 2022-09-19

C#读取ActiveDirectory(曹海涛)

C#读取ActiveDirectory(曹海涛)

public class ActiveDirectoryManager

{

public static DirectoryEntry GetDirectoryEntry()

{

DirectoryEntry entry = null;

try

{

if (entry == null)

{

entry = new DirectoryEntry(ActiveDirectoryHelper.ADString, ActiveDirectoryHelper.ADUserName, ActiveDirectoryHelper.ADPassWord, AuthenticationTypes.Secure);

}

return entry;

}

catch (Exception ex)

{

throw ex;

}

}

public static DirectorySearcher GetDirectorySearcher(DirectoryEntry entry)

{

DirectorySearcher searcher = null;

try

{

if (searcher == null)

{

searcher = new DirectorySearcher(entry)

{

CacheResults = true,

SearchScope = SearchScope.Subtree

};

var propertys = ActiveDirectoryHelper.ADProperty.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

searcher.PropertiesToLoad.Clear();

foreach (var item in propertys)

{

searcher.PropertiesToLoad.Add(item);

}

}

return searcher;

}

catch (Exception ex)

{

throw ex;

}

}

}

public class ActiveDirectoryHelper

{

public static string ADString = ConfigurationManager.ConnectionStrings["ADConnectionString"].ConnectionString;

public static string ADUserName = ConfigurationManager.AppSettings["ADUsername"];

public static string ADPassWord = ConfigurationManager.AppSettings["ADPassword"];

public static string ADProperty = ConfigurationManager.AppSettings["ADProperty"];

public static string ADKey = ConfigurationManager.AppSettings["ADKey"];

public static string ADUserWhere = ConfigurationManager.AppSettings["ADUserWhere"].Replace("﹠", "&");

public static List GetDirectoryInfo(DirectorySearcher searcher, string userName = null, string userCode = null)

{

var where = string.Empty;

if (!string.IsNullOrWhiteSpace(userName) || !string.IsNullOrWhiteSpace(userCode))

{

where = string.Format(ADUserWhere, userName == null ? string.Empty : userName, userCode == null ? string.Empty : userCode);

}

else

{

where = "(&(objectClass=user))";

}

return ExecuteDataSource(searcher, where);

}

private static List ExecuteDataSource(DirectorySearcher searcher, string where)

{

if (!string.IsNullOrWhiteSpace(where)) searcher.Filter = where;

SearchResultCollection result = searcher.FindAll();

Func basicWhere = x => true;

var keys = ADKey.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

foreach (var item in keys)

{

basicWhere += x => !string.IsNullOrWhiteSpace(x.Properties.Get(item));

}

//return result.OfType().Where(x => !ResultOrEmpty(x)).Select(x => GetDynamic(x)).ToList();

//var SearchResults = result.OfType().Where(basicWhere);

var propertys = ADProperty.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

return result.OfType().Select(x => GetDynamic(x, propertys)).Where(x=>x != null).ToList();

}

private static object GetDynamic(SearchResult result,string[] propertys)

{

dynamic info = new ExpandoObject();

var dic = (IDictionary)info;

//var propertys = ADProperty.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

var keys = ADKey.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

foreach (var item in propertys)

{

if (string.IsNullOrWhiteSpace(result.Properties.Get(item)) && keys.Contains(item))

{

return null;

}

dic.Add(item, result.Properties.Get(item));

}

return info;

}

private static bool ResultOrEmpty(SearchResult result)

{

bool isEmpty = false;

var keys = ADKey.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

foreach (var item in keys)

{

if (string.IsNullOrWhiteSpace(result.Properties.Get(item)))

{

isEmpty = true;

break;

}

}

return isEmpty;

}

}

public static class ResultPropertyCollectionExtension

{

public static T Get(this ResultPropertyCollection target, string propertyName)

{

if (target == null)

throw new ArgumentNullException("target");

if (string.IsNullOrEmpty(propertyName))

throw new ArgumentNullException("propertyName");

if (target.Contains(propertyName) && target[propertyName].Count > 0)

return (T)target[propertyName][0];

return default(T);

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:将vue项目打包成windows下运行的exe应用
下一篇:#导入MD文档图片# Windows 系统安装使用 Nginx
相关文章

 发表评论

暂时没有评论,来抢沙发吧~