[英]How to know if DirectoryEntry is a user or a group?
Hi,你好,
I have the following code to create a tree from the current AD :我有以下代码可以从当前 AD 创建一棵树:
public static ActiveDirectory GetActiveDirectoryTree(string pathToAD = "")
{
DirectoryEntry objADAM = default(DirectoryEntry);
// Binding object.
DirectoryEntry objGroupEntry = default(DirectoryEntry);
// Group Results.
DirectorySearcher objSearchADAM = default(DirectorySearcher);
// Search object.
SearchResultCollection objSearchResults = default(SearchResultCollection);
// Binding path.
ActiveDirectory result = new ActiveDirectory();
ActiveDirectoryItem treeNode;
// Get the AD LDS object.
try
{
if (pathToAD.Length > 0)
objADAM = new DirectoryEntry();
else
objADAM = new DirectoryEntry(pathToAD);
objADAM.RefreshCache();
}
catch (Exception e)
{
throw e;
}
// Get search object, specify filter and scope,
// perform search.
try
{
objSearchADAM = new DirectorySearcher(objADAM);
objSearchADAM.Filter = "(&(objectClass=group))";
objSearchADAM.SearchScope = SearchScope.Subtree;
objSearchResults = objSearchADAM.FindAll();
}
catch (Exception e)
{
throw e;
}
// Enumerate groups
try
{
if (objSearchResults.Count != 0)
{
//SearchResult objResult = default(SearchResult);
foreach (SearchResult objResult in objSearchResults)
{
objGroupEntry = objResult.GetDirectoryEntry();
result.ActiveDirectoryTree.Add(new ActiveDirectoryItem() { Id = objGroupEntry.Guid, ParentId = objGroupEntry.Parent.Guid, AccountName = objGroupEntry.Name, Type = ActiveDirectoryType.Group, PickableNode = false });
foreach (object child in objGroupEntry.Properties["member"])
{
treeNode = new ActiveDirectoryItem();
var path = "LDAP://" + child.ToString().Replace("/", "\\/");
using (var memberEntry = new DirectoryEntry(path))
{
if (memberEntry.Properties.Contains("sAMAccountName") && memberEntry.Properties.Contains("objectSid"))
{
treeNode.Id = Guid.NewGuid();
treeNode.ParentId = objGroupEntry.Guid;
treeNode.AccountName = memberEntry.Properties["sAMAccountName"][0].ToString();
treeNode.Type = ActiveDirectoryType.User;
treeNode.PickableNode = true;
treeNode.FullName = memberEntry.Properties["Name"][0].ToString();
byte[] sidBytes = (byte[])memberEntry.Properties["objectSid"][0];
treeNode.ObjectSid = new System.Security.Principal.SecurityIdentifier(sidBytes, 0).ToString();
result.ActiveDirectoryTree.Add(treeNode);
}
}
}
}
}
else
{
throw new Exception("No groups found");
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return result;
}
The problem is that using (var memberEntry = new DirectoryEntry(path)) returns DomainUsers as a user to this tree and Im not sure if this is correct?问题是使用 (var memberEntry = new DirectoryEntry(path)) 将 DomainUsers 作为用户返回到这棵树,我不确定这是否正确?
Say that I store the sidId for the DomainUsers node and then sends it to the following method :假设我存储了 DomainUsers 节点的 sidId,然后将其发送到以下方法:
public static Boolean GetActiveDirectoryName(string sidId,out string samAccountName,out string fullName)
{
samAccountName = string.Empty;
fullName = string.Empty;
if (sidId != null && sidId.Length > 0)
{
var ctx = new System.DirectoryServices.AccountManagement.PrincipalContext(ContextType.Domain, null);
using (var up = UserPrincipal.FindByIdentity(ctx, IdentityType.Sid, sidId))
{
samAccountName = up.SamAccountName;
fullName = up.Name;
return true;
}
}
return false;
}
The up will be set to null? up会被设置为null吗? If I choose another user in the AD then it workes just fine.如果我在 AD 中选择另一个用户,那么它工作得很好。 I suspect that the DomainUsers is a group, but how do I check for this on then DirectoryEntry?我怀疑 DomainUsers 是一个组,但是我如何在 DirectoryEntry 上检查这个?
BestRegards此致
Off the top of my head: Have you considered checking Schema properties of the returned result?在我的头顶上:您是否考虑过检查返回结果的 Schema 属性? I'm thinking you could easily figure a group by using DirectoryEntry.SchemaEntry.Name
.我认为您可以通过使用DirectoryEntry.SchemaEntry.Name
轻松找到一个组。 It should return group
if your schema entry is a group.它应该返回group
,如果你的模式条目是一组。
Reference: MSDN: DirectoryEntry.SchemaEntry参考: MSDN:DirectoryEntry.SchemaEntry
if (pathToAD.Length > 0) objADAM = new DirectoryEntry(); else objADAM = new DirectoryEntry(pathToAD); objADAM.RefreshCache();
wouldn't you want to use pathToAD
IF the Length>0
?如果Length>0
您不想使用pathToAD
吗?
Warning :警告:
The accepted answer is dangerous to use since the DirectoryEntry.SchemaEntry.Name
might be anything.接受的答案使用起来很危险,因为DirectoryEntry.SchemaEntry.Name
可能是任何东西。 (See here for more details). (有关更多详细信息,请参见此处)。
So, the easiest way would be to check the objectClass
instead, like this:因此,最简单的方法是检查objectClass
,如下所示:
// For group check
bool isGroup = entry.Properties["objectClass"]?.Contains("group") == true;
// For user check
bool isUser = entry.Properties["objectClass"]?.Contains("user") == true;
PS For those who are wondering why I've used == true
, see here PS 对于那些想知道为什么我使用== true
,请参阅此处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.