如何确定用户帐户是否启用或禁用
我正在编写一个快速的 C# win 表单应用程序来帮助解决重复性的文书工作。
我已在 AD 中搜索所有用户帐户,并将它们添加到带有复选框的列表视图中。
我想默认 listviewitems 的默认检查状态取决于帐户的启用/禁用状态。
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 am throwing together a quick C# win forms app to help resolve a repetitive clerical job.
I have performed a search in AD for all user accounts and am adding them to a list view with check boxes.
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);
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里的代码应该可以工作...
this code here should work...
使用 System.DirectoryServices.AccountManagement:
域名和用户名必须是域名和用户名的字符串值。
Using System.DirectoryServices.AccountManagement:
domainName and username must be the string values of the domain and username.
没有人问过,但这里有一个 java 版本(因为我最终在这里寻找一个)。空检查留给读者作为练习。
Not that anyone asked, but here's a java version (since I ended up here looking for one). Null checking is left as an exercise for the reader.
您可以使用如下内容:
这是所有可能标志的完整列表:
You can use something like this:
Here is a complete list of all possible flags:
我来这里寻找答案,但它只是针对
DirectoryEntry
。因此,这里有一个适用于SearchResult
/SearchResultCollection
的代码,供遇到相同问题的人使用:I came here looking for an answer, but it was only for
DirectoryEntry
. So here is a code that works forSearchResult
/SearchResultCollection
, for people who had the same problem: