[英]C# ASP.NET - Search AD for active and 'suspended' users
我有一个脚本,将在用户请求新用户时执行。
此脚本将使用用户提供的用户名,并搜索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;
}
}
可能值得注意的是,我没有编写此代码并且它已经存在。
任何帮助深表感谢。
这是一些适合您的代码。
首先,将“accountExpires”添加到PropertiesToLoad
,然后您可以使用它来查看帐户是否已过期:
var isExpired = false;
if (sr.Properties.Contains("accountExpires")) {
var expiry = (long)sr.Properties["accountExpires"][0];
if (!expiry.Equals(9223372036854775807) && !expiry.Equals(0)) {
isExpired = DateTime.FromFileTime(expiry) <= DateTime.Now;
}
}
神奇的数字是因为,正如文档所述 ,“值为0或0x7FFFFFFFFFFFFFFF(9223372036854775807)表示该帐户永不过期。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.