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

How to determine if user account is enabled or disabled(如何确定用户帐户是启用还是禁用)
本文介绍了如何确定用户帐户是启用还是禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在拼凑一个快速的 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);
}

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

ActiveDirectory error 0x8000500c when traversing properties(遍历属性时 ActiveDirectory 错误 0x8000500c)
search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
Get the list of Groups for the given UserPrincipal(获取给定 UserPrincipal 的组列表)
Can you find an Active Directory User#39;s Primary Group in C#?(你能在 C# 中找到 Active Directory 用户的主要组吗?)
Query From LDAP for User Groups(从 LDAP 查询用户组)
How can I get DOMAINUSER from an AD DirectoryEntry?(如何从 AD DirectoryEntry 获取 DOMAINUSER?)