c# - 如何从 LDAP 路径字符串中提取项目

标签 c# string split active-directory ldap

是否有一种“正确”的方式从以下字符串中检索例如 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/

相关文章:

java - 通过递归查找所有可能的长度为 k 的字符串

java - 匹配字符串中第一个逗号之后的所有内容

c - 如何在C中定义非常大的二维数组(例如1000*1000)

postgresql - 如何在 psql 中将一列拆分为更多列?

c# - 如何检索数据网格中数据绑定(bind)文本框的更改值

c# - 对路径 'Global\{xxx}_YYY-YYY:13552' 的访问被拒绝。吊火?

c# - 使用 Linq 对列表元素进行分组并映射到新模型

c# - Unity 2018.3 的 Android 运行时权限

mysql获取字符左侧的字符串并将其与另一列进行比较

c# - 使用定界符拆分字符串,但在 C# 中保留结果中的定界符