目前,我们有一个 SQL Server 视图,可以查询 AD 并返回有关所有组的信息。
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
某些组 ADsPath 值包含 - 字符,例如 LDAP://CN=EXCHANGE – CARGO,CN=Users,DC=mycompany,DC=co,DC=uk
我现在使用 C# 和 DirectoryServices 替换此 SQL Server 视图,如下所示:
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;
}
}
问题是 DirectoryServices 将 - 字符返回为 –
我需要做什么才能使 DirectoryServices 将 - 字符返回为 -
而不是作为 –
谢谢您的帮助。