简体   繁体   中英

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:

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.

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.

I'm using the following in a list in 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. 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

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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