C# LdapConnection SearchRequest为“引发异常”;超出了尺寸限制;
由于需要使用LDAPS连接到LDAP服务器,因此必须使用LdapConnection而不是DirectoryEntry 以下是源代码:C# LdapConnection SearchRequest为“引发异常”;超出了尺寸限制;,c#,.net,ldap,C#,.net,Ldap,由于需要使用LDAPS连接到LDAP服务器,因此必须使用LdapConnection而不是DirectoryEntry 以下是源代码: SearchResponse response; using (LdapConnection con = new LdapConnection(new LdapDirectoryIdentifier(Host, Port))) { if (IsSSL) {
SearchResponse response;
using (LdapConnection con = new LdapConnection(new LdapDirectoryIdentifier(Host, Port)))
{
if (IsSSL)
{
con.SessionOptions.SecureSocketLayer = true;
con.SessionOptions.VerifyServerCertificate =
(connection, certificate)
=> true;
}
con.Credential = new NetworkCredential(_username, _password);
con.AuthType = AuthType.Basic;
con.Bind();
if (logMessage != null)
logMessage("Connected to LDAP");
string sFilter = String.Format(
"(&(objectcategory=person)(objectclass=user){0}(!(userAccountControl:1.2.840.113556.1.4.803:=2)))",
filter
);
SearchRequest request = new SearchRequest("OU=Corp,DC=mydc,DC=com", sFilter, SearchScope.Subtree);
request.Attributes.Add(Resources.objectguid);
request.Attributes.Add(Resources.givenname);
request.Attributes.Add(Resources.sn);
request.Attributes.Add(Resources.initials);
request.Attributes.Add(Resources.samaccountname);
request.Attributes.Add(Resources.userprincipalname);
request.Attributes.Add(Resources.mail);
request.Attributes.Add(Resources.objectsid);
request.Attributes.Add(Resources.department);
request.Attributes.Add(Resources.company);
request.SizeLimit = 10;
response = (SearchResponse) con.SendRequest(request);
}
在执行源代码时(我们已使用外部第三方软件验证了凭据、主机、端口等),我们得到以下异常:
超出了大小限制
说明:执行当前web请求期间发生未经处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源
异常详细信息:System.DirectoryServices.Protocols.DirectoryOperationException:超出了大小限制
源错误:
response = (SearchResponse) con.SendRequest(request);
messageId、LdapOperation操作、ResultAll resultType、TimeSpan
requestTimeOut,布尔异常超时)+2385
System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest
请求,TimeSpan请求超时)+499
System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest
请求)+50
UserSearchProvider.ADUserSearchProvider.QueryStore(UserSearchCriteriaCollection
c:\Users\stemarie\Documents\Visual中的标准,操作1日志消息)
演播室
2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\ADUserSearchProvider.cs:298
UserSearchProvider.UserSearchProvider.QueryAndSort(UserSearchCriteriaCollection
c:\Users\stemarie\Documents\Visual中的标准、操作
1日志消息)
演播室
2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\UserSearchProvider.cs:77
UserSearchProvider.UserSearchProvider.Search(UserSearchCriteriaCollection
c:\Users\stemarie\Documents\Visual中的标准,操作1日志消息)
演播室
2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\UserSearchProvider.cs:33
UserSearchProvider.UserSearchService.Search(UserSearchCriteriaCollection
c:\Users\stemarie\Documents\Visual中的标准、操作
1日志消息)
演播室
2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\UserSearchService.cs:44
UserSearchProviderTest.\默认值。页面\在c:\Users\stemarie\Documents\Visual Studio中加载(对象发送者,事件参数e)
2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProviderTest\Default.aspx.cs:28
让我困惑的是,我们确实指定了最大大小限制,我们不想要超过100个条目——我们想要限制它。但是,即使我们指定SizeLimit为1,库也会始终抛出错误
是否有人对此问题有任何见解/建议?我们很快就能解决这一问题,只需要解决最后一个问题。事实证明,这一点很有效:
try
{
response = (SearchResponse)con.SendRequest(request);
return response.Entries.Cast<SearchResultEntry>()
.Select(entry => entry.Attributes)
.Select(x => GetADUserSearchItemFromADProperties(x, logMessage))
.Where(user => user.HasName)
.Cast<IUserSearchItem>();
}
catch (DirectoryOperationException ex)
{
response = (SearchResponse) ex.Response;
return response.Entries.Cast<SearchResultEntry>()
.Select(entry => entry.Attributes)
.Select(x => GetADUserSearchItemFromADProperties(x, logMessage))
.Where(user => user.HasName)
.Cast<IUserSearchItem>();
}
试试看
{
响应=(搜索响应)con.SendRequest(请求);
返回response.Entries.Cast()
.Select(entry=>entry.Attributes)
.Select(x=>GetADUserSearchItemFromADProperties(x,logMessage))
.Where(user=>user.HasName)
.Cast();
}
捕获(目录操作例外)
{
响应=(搜索响应)例如响应;
返回response.Entries.Cast()
.Select(entry=>entry.Attributes)
.Select(x=>GetADUserSearchItemFromADProperties(x,logMessage))
.Where(user=>user.HasName)
.Cast();
}
MSDN文档声明您将获得一个类作为DirectoryOperationException.Response属性。但是,您可以将此属性强制转换为类型,然后使用SearchResponse.Entries属性获取在命中指定的SizeLimit之前收到的条目
我已经尝试过了,并且得到了预期的结果,我只是有点不安,因为我必须处理异常才能执行该操作。您应该在类似于此的函数中使用cookies。 该函数返回SearchResponse对象的集合,调用方应循环该集合
private List<SearchResponse> SearchDirectory(string distinguishedName, string searchFilter, System.DirectoryServices.Protocols.SearchScope searchScope, params string[] attributeList)
{
List<SearchResponse> result = new List<SearchResponse>();
SearchResponse response = null;
int maxResultsToRequest = 100;
try
{
PageResultRequestControl pageRequestControl = new PageResultRequestControl(maxResultsToRequest);
// used to retrieve the cookie to send for the subsequent request
PageResultResponseControl pageResponseControl;
SearchRequest searchRequest = new SearchRequest(distinguishedName, searchFilter, searchScope, attributeList);
searchRequest.Controls.Add(pageRequestControl);
while (true)
{
response = (SearchResponse)connection.SendRequest(searchRequest);
result.Add(response);
pageResponseControl = (PageResultResponseControl)response.Controls[0];
if (pageResponseControl.Cookie.Length == 0)
break;
pageRequestControl.Cookie = pageResponseControl.Cookie;
}
}
catch (Exception e)
{
// do something with the error
}
return result;
}
private List SearchDirectory(字符串区分名称、字符串searchFilter、System.DirectoryServices.Protocols.SearchScope、参数字符串[]属性列表)
{
列表结果=新列表();
SearchResponse=null;
int maxResultsToRequest=100;
尝试
{
PageResultRequestControl pageRequestControl=新的PageResultRequestControl(maxResultsToRequest);
//用于检索要为后续请求发送的cookie
PageResultResponse控件PageResponse控件;
SearchRequest SearchRequest=新的SearchRequest(区分名称、searchFilter、searchScope、attributeList);
searchRequest.Controls.Add(pageRequestControl);
while(true)
{
response=(SearchResponse)connection.SendRequest(searchRequest);
结果:添加(响应);
pageResponseControl=(PageResultResponseControl)响应。控件[0];
if(pageResponseControl.Cookie.Length==0)
打破
pageRequestControl.Cookie=pageResponseControl.Cookie;
}
}
捕获(例外e)
{
//对这个错误做点什么
}
返回结果;
}
有多少目录条目与您的搜索匹配?如果删除SizeLimit行,会发生什么?如果sizelimit的值低于服务器页面大小,则它将失败。我想你应该尝试发出分页搜索。我确实在请求中发出了分页搜索控件,但得到的响应是服务器不支持分页搜索。我不知道有多少条目可以匹配我的搜索,但我只想要100条。代码的目的是创建一个执行“用户”操作的组件
private List<SearchResponse> SearchDirectory(string distinguishedName, string searchFilter, System.DirectoryServices.Protocols.SearchScope searchScope, params string[] attributeList)
{
List<SearchResponse> result = new List<SearchResponse>();
SearchResponse response = null;
int maxResultsToRequest = 100;
try
{
PageResultRequestControl pageRequestControl = new PageResultRequestControl(maxResultsToRequest);
// used to retrieve the cookie to send for the subsequent request
PageResultResponseControl pageResponseControl;
SearchRequest searchRequest = new SearchRequest(distinguishedName, searchFilter, searchScope, attributeList);
searchRequest.Controls.Add(pageRequestControl);
while (true)
{
response = (SearchResponse)connection.SendRequest(searchRequest);
result.Add(response);
pageResponseControl = (PageResultResponseControl)response.Controls[0];
if (pageResponseControl.Cookie.Length == 0)
break;
pageRequestControl.Cookie = pageResponseControl.Cookie;
}
}
catch (Exception e)
{
// do something with the error
}
return result;
}