C# 如何遍历具有可分辨名称的目录
如果我有一个目录路径(以及相应的凭据),如: 我可以使用哪些类/方法通过其可分辨名称(DN)属性访问各种对象?我尝试了C# 如何遍历具有可分辨名称的目录,c#,.net,active-directory,ldap,C#,.net,Active Directory,Ldap,如果我有一个目录路径(以及相应的凭据),如: 我可以使用哪些类/方法通过其可分辨名称(DN)属性访问各种对象?我尝试了DirectoryEntry类,但没有找到任何方法从“base”对象和DN检索DirectoryEntry对象 示例:拥有上面的目录URL和DNCN=User,OU=Development,DC=domain,DC=com,如何访问LDAP://Directory:389/CN=User,OU=Development,DC=domain,DC=com的DirectoryEntry
DirectoryEntry
类,但没有找到任何方法从“base”对象和DN检索DirectoryEntry
对象
示例:拥有上面的目录URL和DNCN=User,OU=Development,DC=domain,DC=com
,如何访问LDAP://Directory:389/CN=User,OU=Development,DC=domain,DC=com
的DirectoryEntry
(或类似)对象
我见过一些涉及URL字符串操作的解决方案,但我正在寻找一种方法,用普通的.NET对象/类来实现这一点。您是否可以使用DirectorySearcher
从根目录DirectoryEntry
中搜索子树
正如你在评论中指出的那样,这不会有帮助。我认为解析DirectoryEntry.Path、提取所需位、连接DN、转义DN中的任何特殊字符(如果需要)都是无可替代的
您需要包括第三条斜线在内的所有内容:
LDAP://server:port/relativePath
如果使用无服务器绑定,则使用第二个斜杠:
LDAP://relativePath
但如果relativePath本身包含斜杠(将用反斜杠转义),则可能存在歧义。因此,正如您所说,字符串操作。您是否可以使用DirectorySearcher
从根目录DirectoryEntry
中搜索子树
正如你在评论中指出的那样,这不会有帮助。我认为解析DirectoryEntry.Path、提取所需位、连接DN、转义DN中的任何特殊字符(如果需要)都是无可替代的
您需要包括第三条斜线在内的所有内容:
LDAP://server:port/relativePath
如果使用无服务器绑定,则使用第二个斜杠:
LDAP://relativePath
但如果relativePath本身包含斜杠(将用反斜杠转义),则可能存在歧义。因此,正如您所说,字符串操作。所有内容都已解释
以下是一个例子:
DirectoryEntry userEntry = new DirectoryEntry("LDAP://directory:389/CN=User,OU=Development,DC=domain,DC=com",
"<adminAccountName>",
"<adminPassword");
DirectoryEntry userEntry=newdirectoryentry(“LDAP://directory:389/CN=User,OU=Development,DC=domain,DC=com”,
“所有内容都已解释
以下是一个例子:
DirectoryEntry userEntry = new DirectoryEntry("LDAP://directory:389/CN=User,OU=Development,DC=domain,DC=com",
"<adminAccountName>",
"<adminPassword");
DirectoryEntry userEntry=newdirectoryentry(“LDAP://directory:389/CN=User,OU=Development,DC=domain,DC=com”,
“以下是我目前使用的不太理想的方法:
string GetNewDN(DirectoryEntry deBase, string DN)
{
try
{ // Handle the LDAP://example.com:389/DN=string formats
return (new Uri(deBase.Path)).GetLeftPart(UriPartial.Authority) + "/" + DN.Replace("/", @"\/");
}
catch (UriFormatException)
{ // Handle the LDAP://DN=string formats
return deBase.Path.Substring(0, deBase.Path.IndexOf(":")) + "://" + DN.Replace("/", @"\/");
}
}
以下是我目前使用的不太理想的方法:
string GetNewDN(DirectoryEntry deBase, string DN)
{
try
{ // Handle the LDAP://example.com:389/DN=string formats
return (new Uri(deBase.Path)).GetLeftPart(UriPartial.Authority) + "/" + DN.Replace("/", @"\/");
}
catch (UriFormatException)
{ // Handle the LDAP://DN=string formats
return deBase.Path.Substring(0, deBase.Path.IndexOf(":")) + "://" + DN.Replace("/", @"\/");
}
}
我没有确切的LDAP路径;从一个DirectoryEntry
,我如何从DN中检索另一个DirectoryEntry
?因此,如果我很清楚,您的问题只是将DC=domain,DC=com(DN的“DC”部分)转换为DNS名称以构建“目录”你的URL的一部分?它只是一个字符串操作。我没有确切的LDAP路径;从一个DirectoryEntry
,我如何从DN中检索另一个DirectoryEntry
?因此,如果我理解得很好,你的问题只是将DC=domain,DC=com(DN的“DC”部分)转换为DNS名称以构建“目录”你的URL的一部分?它只是一个字符串操作。也许;我该怎么做?newdirectorysearcher(DE.Filter=“(&(DN=CN=User,OU=Development,DC=domain,DC=com))”
?我尝试过类似的方法,但没有效果。如果我可以使用DirectorySearcher
查找我的结果,那么我将拥有所需的一切,因为SearchResult
对象具有GetDirectoryEntry()
member function.@palswim-没错,dn不是这样使用的属性。因此我认为您需要使用URL-您描述的字符串操作只是串联:“LDAP://server:port/”+discrimitedName,或者如果您正在使用无服务器绑定到AD,“LDAP://”+DifferentizedNamemaybe;我该怎么做?new DirectorySearcher(DE).Filter=“(&(DN=CN=User,OU=Development,DC=domain,DC=com))”
?我尝试过类似的方法,但没有效果。如果我可以使用DirectorySearcher
查找我的结果,那么我将拥有所需的一切,因为SearchResult
对象具有GetDirectoryEntry()
member function.@palswim-没错,dn不是这样使用的属性。因此我认为您需要使用URL-您描述的字符串操作只是串联:“LDAP://server:port/”+discrimitedName,或者如果您正在使用无服务器绑定到AD,“LDAP://”+DifferentiedName查看,如果它没有提供您想要的,请提供!:)查看,如果它没有提供您想要的,请提供!:)