使用以下内容:
string distinguishedName = deUser.Properties["distinguishedName"].Value.ToString();
转储到列表为我提供了测试用户的正确 DN:
CN=Test User,OU=PHL,OU=_users,DC=testdomain,DC=local
我希望输出以对象所在路径的形式读取,如:
testdomain.local/_users/PHL
我在 stackoverflow 上进行了搜索,它使用仅显示父 OU 的方法返回了相当多的过时旧帖子,但没有显示任何路径。
如何将上面的 DN 转换为字符串路径,不知道如何拆分和连接它,因为我基本上需要将 DN 从右向左反转,中间用“/”替换逗号。
我在 C#.Net 的列表中使用以下内容...
UsersList.Add(new UserAttributes
{
Id = id++,
FirstName = FirstName,
MiddleInitial = MiddleInitial,
LastName = LastName,
SAMAccountName = samaccountName,
description = Description,
DistinguishedName = distinguishedName,
email = email
});
return UsersList;
任何有关 javascript 或 C# 的帮助将不胜感激。我正在使用数据表,因此我可以使用列中的“渲染”函数进行拆分和连接。
如果有人可以用 javascript 或 c# 开始使用简单的字符串
string distName = "CN=Test User,OU=PHL,OU=_users,DC=testdomain,DC=local";
或一个
var distName = "CN=Test User,OU=PHL,OU=_users,DC=testdomain,DC=local";
并向我展示如何拆分、连接、反转它,我应该可以将它变成一个函数来使用。
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
string test = "CN=Test User,OU=PHL,OU=_users,DC=testdomain,DC=local";
List<string> splitted = new List<string>(test.Split(','));
int ouCount = 0;
for(int i = 0; i < splitted.Count; i++)
{
string[] split = splitted[i].Split('=');
splitted[i] = split[1];
if(split[0] == "OU")
{
ouCount++;
}
}
string result = splitted[splitted.Count - 2] + "." + splitted[splitted.Count - 1];
for(int i = 0; i < ouCount; i++)
{
result += "/" + splitted[i + 1];
}
Console.WriteLine(result);
}
}
对于拆分,您可以使用 string.Split() 函数。您写道您想反转您的列表,但您给定的输出示例与此不匹配。
使用
String.split()
按 comma (,)
进行分割,然后使用模板文字格式化路径输出。
var distName = "CN=Test User,OU=PHL,OU=_users,DC=testdomain,DC=local";
const getValue = (input) => input.split("=").pop();
const formattedPath = (str) => {
const list = str.split(",");
const len = list.length;
return `${getValue(list[len-2])}.${getValue(list[len-1])}/${getValue(list[len-3])}/${getValue(list[len-4])}`;
}
console.log(formattedPath(distName));