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

时间:2022-11-10
本文介绍了如何确定用户帐户是启用还是禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在拼凑一个快速的 C# win 表单应用程序来帮助解决重复的文书工作.

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?

推荐答案

这里的代码应该可以工作...

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);
}

这篇关于如何确定用户帐户是启用还是禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何获得“公司"和“部门"从 Active Directory 给定 UserPrincipal 对 下一篇:如何获取已登录 Windows 用户的名字和姓氏?

相关文章

最新文章