app开发者平台在数字化时代的重要性与发展趋势解析
654
2022-09-19
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
{
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
{
if (!string.IsNullOrWhiteSpace(where)) searcher.Filter = where;
SearchResultCollection result = searcher.FindAll();
Func
var keys = ADKey.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in keys)
{
basicWhere += x => !string.IsNullOrWhiteSpace(x.Properties.Get
}
//return result.OfType
//var SearchResults = result.OfType
var propertys = ADProperty.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
return result.OfType
}
private static object GetDynamic(SearchResult result,string[] propertys)
{
dynamic info = new ExpandoObject();
var dic = (IDictionary
//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
{
return null;
}
dic.Add(item, result.Properties.Get
}
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
{
isEmpty = true;
break;
}
}
return isEmpty;
}
}
public static class ResultPropertyCollectionExtension
{
public static T Get
{
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小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~