Lea*_*pez 26
试试这个:
class Program
{
static void Main(string[] args)
{
const string ldap = "LDAP://your-ldap-server-here";
using (DirectoryEntry conn = new DirectoryEntry(ldap))
{
using (DirectorySearcher searcher = new DirectorySearcher(conn))
{
searcher.Filter = "(|(samAccountName=userA)(samAccountName=userB))";
searcher.PropertiesToLoad.Add("samAccountName");
searcher.PropertiesToLoad.Add("userAccountControl");
using (SearchResultCollection results = searcher.FindAll())
{
foreach (SearchResult result in results)
{
int userAccountControl = Convert.ToInt32(result.Properties["userAccountControl"][0]);
string samAccountName = Convert.ToString(result.Properties["samAccountName"][0]);
bool disabled = ((userAccountControl & 2) > 0);
Console.WriteLine("{0} ({1:x}) :: {2}", samAccountName, userAccountControl, disabled);
}
}
}
}
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
userAccountControl如果帐户被禁用,则第二位将为1.
小智 6
如果您使用的是.NET 3.5,则可以使用新的System.DirectoryServices.AccountManagment命名空间方法来更轻松地访问Active Directory.UserPrincipal对象具有Enabled属性,可以为您提供所需内容.
在2008年1月的MSDN杂志中,对这些例程有了很好的概述.您可以在此处在线阅读文章:在.NET Framework 3.5中管理目录安全性主体
| 归档时间: |
|
| 查看次数: |
24420 次 |
| 最近记录: |