是否有一种“正确”的方式从以下字符串中检索例如 CN:
“LDAP://CN=名字姓氏,OU=域管理员,DC=DOMAIN1,DC=co,DC=uk”
我从 DirectorySearcher
检索到的内容
目前我正在这样做:
var name = result.Path.Split('=')[1].Split(',')[0];
但这感觉不是最好的方法 - 有人知道任何替代方法吗?
最佳答案
您可以查看这篇文章:An RFC 2253 Compliant Distinguished Name Parser
There are three main classes in this code:
- DN, which represents a full distinguished name
- RDN, which represents a relative distinguished name
RDNComponent, which represents the individual components of a multivalued RDN
DN myDN = new DN(@"CN=Pete Everett\, esq.,OU=People,DC=example,DC=com");
To print out a DN object, you use its ToString() method, as you'd expect.
Console.WriteLine(myDN.ToString()); // prints out: // CN=Pete Everett\, esq.,OU=People,DC=example,DC=com
But if you'd like more control over the formatting, you can specify categories of characters to escape.
Console.WriteLine(myDN.ToString(EscapeChars.None)); // prints out: // CN=Pete Everett, esq.,OU=People,DC=example,DC=com // (Note that this is an incorrect DN format, and will not parse correctly.)
To get the parent object of a given DN object, you can use its Parent property.
DN myParentDN = myDN.Parent; Console.WriteLine(myParentDN.ToString()); // prints out: // OU=People,DC=example,DC=com
关于c# - 如何从 LDAP 路径字符串中提取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36643242/