如何确定是否用户帐户是启用还是禁用帐户、用户

2023-09-09 21:32:59 作者:我爱陈奕迅!

我扔一起快速的C#赢形式的应用程序,以帮助解决一个重复的文职工作。

I am throwing together a quick C# win forms app to help resolve a repetitive clerical job.

我已经执行了AD为所有用户帐户的搜索和我将它们添加到列表视图复选框。

I have performed a search in AD for all user accounts and am adding them to a list view with check boxes.

我想违约的listviewitems默认勾选状态取决于帐户的启用/禁用状态。

I would like to default the listviewitems' default check state to depend upon the enabled/disabled state of the account.

string path = "LDAP://dc=example,dc=local";
DirectoryEntry directoryRoot = new DirectoryEntry(path);
DirectorySearcher searcher = new DirectorySearcher(directoryRoot,
    "(&(objectClass=User)(objectCategory=Person))");
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult result in results)
{
    DirectoryEntry de = result.GetDirectoryEntry();
    ListViewItem lvi = new ListViewItem(
        (string)de.Properties["SAMAccountName"][0]);
    // lvi.Checked = (bool) de.Properties["AccountEnabled"]
    lvwUsers.Items.Add(lvi);
}

我在努力寻找合适的属性来分析,从DirectoryEntry对象得到帐号的状态。我搜索了 AD用户属性,但没有发现任何有用的。

I'm struggling to find the right attribute to parse to get the state of the account from the DirectoryEntry object. I've searched for AD User attributes, but not found anything useful.

任何人都可以提供任何指针?

Can anyone offer any pointers?

推荐答案

这code在这里应该工作...

this code here should work...

private bool IsActive(DirectoryEntry de)
{
  if (de.NativeGuid == null) return false;

  int flags = (int)de.Properties["userAccountControl"].Value;

  return !Convert.ToBoolean(flags & 0x0002);
}