如何确定是否用户帐户是启用还是禁用 [英] How to determine if user account is enabled or disabled

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

问题描述

我扔一起快速的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);
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆