繁体   English   中英

如何在 C# 或 JavaScript 中将 DN 拆分并加入到来自 Active Directory 对象的路径

[英]How to split and join a DN to a path from an Active Directory object in either C# or JavaScript

使用以下内容:

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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM