简体   繁体   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

Using the following:使用以下内容:

string distinguishedName = deUser.Properties["distinguishedName"].Value.ToString();

Dumped to a list is giving me the correct DN of a test user:转储到列表中为我提供了测试用户的正确 DN:

CN=Test User,OU=PHL,OU=_users,DC=testdomain,DC=local

I want the output to read in the form of a path of where the object is located, such as:我希望输出以对象所在路径的形式读取,例如:

testdomain.local/_users/PHL

I've done a search on stackoverflow and it returned quite a bit of old outdated posts using the method of only showing the parent OU, but nothing to a path.我已经在 stackoverflow 上进行了搜索,它使用仅显示父 OU 的方法返回了相当多的旧帖子,但没有任何路径。

How can I convert that DN above to a string path, not sure how to split and join it since I basically need the DN to be reversed from right to left with "/" in between to replace the commas.如何将上面的 DN 转换为字符串路径,不确定如何拆分和加入它,因为我基本上需要将 DN 从右到左反转,中间用“/”替换逗号。

I'm using the following in a list in C#.Net...我在 C#.Net 的列表中使用以下内容...

UsersList.Add(new UserAttributes
{

    Id = id++,
    FirstName = FirstName,
    MiddleInitial = MiddleInitial,
    LastName = LastName,
    SAMAccountName = samaccountName,
    description = Description,                             
    DistinguishedName = distinguishedName,
    email = email                                
});

return UsersList;

Any help in either javascript or C# would be greatly appreciated.非常感谢 javascript 或 C# 中的任何帮助。 I'm using datatables so I can utilize a split and join using the "render" function in the column.我正在使用数据表,因此我可以使用列中的“渲染”功能进行拆分和连接。

If someone can start in either javascript or c# with a simple string of如果有人可以在 javascript 或 c# 中使用一个简单的字符串开始

string distName = "CN=Test User,OU=PHL,OU=_users,DC=testdomain,DC=local";

or a或一个

var distName = "CN=Test User,OU=PHL,OU=_users,DC=testdomain,DC=local";

and show me how to split, join, reverse it, I should be alright turning it into a function to use.并告诉我如何拆分、加入、反转它,我应该可以把它变成一个函数来使用。

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

For splitting you can use string.Split() function.对于拆分,您可以使用 string.Split() 函数。 You wrote that you want to reverse your list, but your given output exmaple doesn't match this.您写道要反转您的列表,但您给定的输出示例与此不匹配。

Use String.split() to split by comma (,) and then format path output using the template literals.使用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