问题描述
由于需要使用LDAPS连接到LDAP服务器,因此必须使用LdapConnection代替DirectoryEntry.
Because of the fact that we are required to connect to an LDAP server using LDAPS we must use LdapConnection instead of DirectoryEntry.
这是源代码:
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);
}
在执行源代码后(我们已经使用外部第三方软件验证了凭据,主机,端口等),我们得到以下异常:
Upon execution of the source code (we have verified credentials, host, port, etc - using an external 3rd party software) we get the following exception:
说明:在执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
异常详细信息::System.DirectoryServices.Protocols.DirectoryOperationException:超出了大小限制
Exception Details: System.DirectoryServices.Protocols.DirectoryOperationException: The size limit was exceeded
源错误:
response = (SearchResponse) con.SendRequest(request);
[DirectoryOperationException: The size limit was exceeded]
System.DirectoryServices.Protocols.LdapConnection.ConstructResponse(Int32
messageId,LdapOperation操作,ResultAll resultType,TimeSpan requestTimeOut,布尔型exceptionOnTimeOut)+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中的Action 1 logMessage) in c:\Users\stemarie\Documents\Visual Studio 2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\ADUserSearchProvider.cs:298 UserSearchProvider.UserSearchProvider.QueryAndSort(UserSearchCriteriaCollection criterias, Action
1 logMessage) 工作室 2012 \ Projects \ Idealink.Modules \ UserSearchProvider \ UserSearchProvider \ UserSearchProvider.cs:77 UserSearchProvider.UserSearchProvider.Search(UserSearchCriteriaCollection 条件,操作c:\ Users \ stemarie \ Documents \ Visual中的Action 1 logMessage) in c:\Users\stemarie\Documents\Visual Studio 2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\UserSearchProvider.cs:33 UserSearchProvider.UserSearchService.Search(UserSearchCriteriaCollection criterias, Action
1 logMessage) 工作室 2012 \ Projects \ Idealink.Modules \ UserSearchProvider \ UserSearchProvider \ UserSearchService.cs:44 UserSearchProviderTest._Default.Page_Load(Object sender,EventArgs e)在c:\ Users \ stemarie \ Documents \ Visual Studio中 2012 \ Projects \ Idealink.Modules \ UserSearchProvider \ UserSearchProviderTest \ Default.aspx.cs:28
messageId, LdapOperation operation, ResultAll resultType, TimeSpan requestTimeOut, Boolean exceptionOnTimeOut) +2385 System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout) +499 System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request) +50 UserSearchProvider.ADUserSearchProvider.QueryStore(UserSearchCriteriaCollection criterias, Action1 logMessage) in c:\Users\stemarie\Documents\Visual Studio 2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\ADUserSearchProvider.cs:298 UserSearchProvider.UserSearchProvider.QueryAndSort(UserSearchCriteriaCollection criterias, Action
1 logMessage) in c:\Users\stemarie\Documents\Visual Studio 2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\UserSearchProvider.cs:77 UserSearchProvider.UserSearchProvider.Search(UserSearchCriteriaCollection criterias, Action1 logMessage) in c:\Users\stemarie\Documents\Visual Studio 2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\UserSearchProvider.cs:33 UserSearchProvider.UserSearchService.Search(UserSearchCriteriaCollection criterias, Action
1 logMessage) in c:\Users\stemarie\Documents\Visual Studio 2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\UserSearchService.cs:44 UserSearchProviderTest._Default.Page_Load(Object sender, EventArgs e) in c:\Users\stemarie\Documents\Visual Studio 2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProviderTest\Default.aspx.cs:28
让我感到困惑的部分是,我们确实指定了最大大小限制,我们不希望超过100个条目-我们想限制它.但是,即使我们将SizeLimit指定为1,库仍会始终引发错误.
The part that confuses me is that we did specify the maximum size limit, we don't want more than 100 entries - we want to limit it. But yet the library consistently throws the error even if we specify a SizeLimit of 1.
有人对此问题有任何见解/建议吗?我们距离这项工作非常接近,只需要解决最后一个问题即可.
Does anyone have any insights/suggestions regarding this issue? We are very close to getting this working and just need to resolve this last problem.
推荐答案
事实证明,这可行:
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>();
}
MSDN文档指出您获得了 DirectoryResponse 类作为DirectoryOperationException.Response属性.但是,您可以将此属性转换为 SearchResponse 类型,然后使用SearchResponse.Entries属性获取在达到指定的SizeLimit之前已收到的条目.
The MSDN documentation states that you get a DirectoryResponse class as the DirectoryOperationException.Response property. You can however cast this property to a SearchResponse type and then use the SearchResponse.Entries property to get the entries that you have received prior to hitting the specified SizeLimit.
我已经尝试过了,但得到了预期的结果,我有点不高兴,必须执行例外操作才能执行该操作.
I have tried this and I get the expected results, I'm just a bit upset that I have to work with an exception in order to perform the operation.
这篇关于LdapConnection SearchRequest会为“超出大小限制"引发异常.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!