我正在尝试将System.DirectoryServices中的搜索重写为System.DirectoryServices.Protocol
在S.DS中我得到了所有请求的属性,但是在S.DS.P中,我没有得到GUID,或者HomePhone ......
其余部分适用于一个用户。
任何想法?
public static List<AllAdStudentsCV> GetUsersDistinguishedName( string domain, string distinguishedName )
{
try
{
NetworkCredential credentials = new NetworkCredential( ConfigurationManager.AppSettings[ "AD_User" ], ConfigurationManager.AppSettings[ "AD_Pass" ] );
LdapDirectoryIdentifier directoryIdentifier = new LdapDirectoryIdentifier( domain+":389" );
using ( LdapConnection connection = new LdapConnection( directoryIdentifier, credentials ) )
{
SearchRequest searchRequest = new SearchRequest( );
searchRequest.DistinguishedName = distinguishedName;
searchRequest.Filter = "(&(objectCategory=person)(objectClass=user)(sn=Afcan))";//"(&(objectClass=user))";
searchRequest.Scope = SearchScope.Subtree;
searchRequest.Attributes.Add("name");
searchRequest.Attributes.Add("sAMAccountName");
searchRequest.Attributes.Add("uid");
searchRequest.Attributes.Add("telexNumber"); // studId
searchRequest.Attributes.Add("HomePhone"); //ctrId
searchRequest.SizeLimit = Int32.MaxValue;
searchRequest.TimeLimit = new TimeSpan(0, 0, 45, 0);// 45 min - EWB
SearchResponse searchResponse = connection.SendRequest(searchRequest) as SearchResponse;
if (searchResponse == null) return null;
List<AllAdStudentsCV> users = new List<AllAdStudentsCV>();
foreach (SearchResultEntry entry in searchResponse.Entries)
{
AllAdStudentsCV user = new AllAdStudentsCV();
user.Active = "Y";
user.CenterName = "";
user.StudId = GetstringAttributeValue(entry.Attributes, "telexNumber");
user.CtrId = GetstringAttributeValue(entry.Attributes, "HomePhone");
user.Guid = GetstringAttributeValue(entry.Attributes, "uid");
user.Username = GetstringAttributeValue(entry.Attributes, "sAMAccountName");
users.Add(user);
}
return users;
}
}
catch (Exception ex)
{
throw;
}
}
另外,如果我想在AD中获取每个用户,那么我可以与我的SQL DB同步数据,我该怎么做,我保持超出最大大小,错误。我将大小设置为maxInt32 ...是否有“忽略大小”选项?
谢谢,
埃里克 -
答案 0 :(得分:1)
我认为标准方法是使用System.DirectoryServices,而不是System.DirectoryServices.Protocol。你为什么要使用后者呢?
关于错误消息“超出最大值”的第二个问题,可能是因为您尝试一次获取太多条目。
Active Directory限制查询返回的对象数,以便不重载目录(限制类似于1000个对象)。获取所有用户的标准方法是使用分页搜索。
算法是这样的: