【发布时间】:2016-03-23 10:28:28
【问题描述】:
我有一个脚本,当用户请求新用户时将执行该脚本。
此脚本将获取用户提供的用户名,并搜索 AD 以查看其是否存在。这绝对没问题,但是我们的 IT 部门在帐户上有到期日期。这会导致帐户处于某种“暂停”状态,而不是被禁用,直到它被移至已离职员工的单独 OU。
C# AD 搜索忽略这些暂停的帐户。
以前有人遇到过这个问题吗?或者有没有人知道如何在搜索中适应这些用户?
public static string ADSearch(string ADPart, string Alias)
{
System.DirectoryServices.DirectoryEntry dirEntry = default(System.DirectoryServices.DirectoryEntry);
System.DirectoryServices.DirectorySearcher dirSearcher = default(System.DirectoryServices.DirectorySearcher);
try
{
dirEntry = new System.DirectoryServices.DirectoryEntry("LDAP://LDAP DETAILS HERE");
dirSearcher = new System.DirectoryServices.DirectorySearcher(dirEntry);
dirSearcher.Filter = "(samaccountname=" + Alias + ")";
dirSearcher.PropertiesToLoad.Add("GivenName");
//Users first name
dirSearcher.PropertiesToLoad.Add("sn");
//Users last name
dirSearcher.PropertiesToLoad.Add("mail");
//Users e-mail
dirSearcher.PropertiesToLoad.Add("samaccountname");
//Samaccount
StringBuilder groupNames = new StringBuilder(); //stuff them in | delimited
SearchResult sr = dirSearcher.FindOne();
//return false if user isn't found
if (sr != null)
if (ADPart == "GivenName")
return sr.Properties["GivenName"][0].ToString().Replace("'", "");
else if (ADPart == "sn")
return sr.Properties["sn"][0].ToString().Replace("'", "");
else if (ADPart == "mail")
return sr.Properties["mail"][0].ToString().Replace("'", "");
else if (ADPart == "alias")
return sr.Properties["samaccountname"][0].ToString().Replace("'", "");
else
return null;
else
return null;
// return false if exception occurs
}
catch (Exception ex)
{
return ex.Message;
}
}
可能值得注意的是,这段代码不是我写的,它已经到位了。
非常感谢任何帮助。
【问题讨论】:
标签: c# asp.net active-directory directoryservices