Bry*_*yan 67 c# directoryservices attributes active-directory
我正在拼凑一个快速的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);
}
Run Code Online (Sandbox Code Playgroud)
我正在努力找到正确的属性进行解析以从DirectoryEntry对象获取帐户的状态.我搜索了AD用户属性,但没有找到任何有用的东西.
谁能提供任何指针?
Dim*_*kis 112
这段代码应该有用......
private bool IsActive(DirectoryEntry de)
{
if (de.NativeGuid == null) return false;
int flags = (int)de.Properties["userAccountControl"].Value;
return !Convert.ToBoolean(flags & 0x0002);
}
Run Code Online (Sandbox Code Playgroud)
小智 10
使用System.DirectoryServices.AccountManagement:domainName和username必须是域和用户名的字符串值.
using (var domainContext = new PrincipalContext(ContextType.Domain, domainName))
{
using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, username))
{
if (foundUser.Enabled.HasValue)
{
return (bool)foundUser.Enabled;
}
else
{
return true; //or false depending what result you want in the case of Enabled being NULL
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 9
不是有人问,但这是一个java版本(因为我最终在这里寻找一个).空检查留给读者练习.
private Boolean isActive(SearchResult searchResult) {
Attribute userAccountControlAttr = searchResult.getAttributes().get("UserAccountControl");
Integer userAccountControlInt = new Integer((String) userAccoutControlAttr.get());
Boolean disabled = BooleanUtils.toBooleanObject(userAccountControlInt & 0x0002);
return !disabled;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
72742 次 |
最近记录: |