简体   繁体   中英

Directory Services Returns Strange Characters

At the moment we've got a SQL Server view that queries AD and returns information about all the Groups.

SELECT  ADsPath, cn, mail, displayname
FROM    OPENQUERY(ADSI, 'SELECT ADsPath, cn,mail,displayname  
                         FROM    ''LDAP://DC=mycompany,DC=co,dc=uk'' 
                         WHERE   objectCategory=''group''    ') AS Rowset_1

Some of the group ADsPath values contain the - character eg LDAP://CN=EXCHANGE – CARGO,CN=Users,DC=mycompany,DC=co,DC=uk

I'm now replacing this SQL Server View using C# and DirectoryServices as follows:

public List<AdGroup> GetGroups()
{
    var groupMembers = new List<AdGroup>();
    using (var ds = new DirectorySearcher(_ldap))
    {
        ds.Filter = String.Format("(&(objectCategory=group))");
        ds.PageSize = 5000;

        ds.PropertiesToLoad.Add("ADsPath");
        ds.PropertiesToLoad.Add("cn");
        ds.PropertiesToLoad.Add("mail");
        ds.PropertiesToLoad.Add("displayName");
        using (var results = ds.FindAll())
        {
            foreach (SearchResult sr in results)
            {                        
                string adsPath = string.Empty;
                if (sr.Properties.Contains("ADsPath"))
                    adsPath = sr.Properties["ADsPath"][0].ToString();

                string cn = string.Empty;
                if (sr.Properties.Contains("cn"))
                    cn = sr.Properties["cn"][0].ToString();

                string mail = string.Empty;
                if (sr.Properties.Contains("mail"))
                    mail = sr.Properties["mail"][0].ToString();

                string displayName = string.Empty;
                if (sr.Properties.Contains("displayName"))
                    displayName = sr.Properties["displayName"][0].ToString();

                DirectoryEntry userAccount = new DirectoryEntry(adsPath);
                string objectGUID = userAccount.Guid.ToString();

                groupMembers.Add(new AdGroup(adsPath, objectGUID, cn, mail, displayName));

            }
        }
        return groupMembers;
    }
}

The problem is DirectoryServices returns the - character as –

What do I need to do to make DirectoryServices return the - character as -

Instead of as –

Thank you for your help.

I was writing the retrieved AD info to a text file so I could check the results. Saving the info in the text file converted all the text into ASCII format, and caused strange characters to be displayed.

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