问题描述
因为我们需要使用 LDAPS 连接到 LDAP 服务器,所以我们必须使用 LdapConnection 而不是 DirectoryEntry.
这里是源代码:
SearchResponse 响应;使用 (LdapConnection con = new LdapConnection(new LdapDirectoryIdentifier(Host, Port))){如果(IsSSL){con.SessionOptions.SecureSocketLayer = true;con.SessionOptions.VerifyServerCertificate =(连接,证书)=>真的;}con.Credential = new NetworkCredential(_username, _password);con.AuthType = AuthType.Basic;con.Bind();如果(logMessage!= null)logMessage("连接到 LDAP");字符串 sFilter = String.Format("(&(objectcategory=person)(objectclass=user){0}(!(userAccountControl:1.2.840.113556.1.4.803:=2)))",筛选);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);请求.SizeLimit = 10;response = (SearchResponse) con.SendRequest(request);}
在执行源代码时(我们已经验证了凭据、主机、端口等 - 使用外部 3rd 方软件),我们得到以下异常:
超出大小限制
说明:在执行当前 Web 请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详情: System.DirectoryServices.Protocols.DirectoryOperationException:超出大小限制
来源错误:
response = (SearchResponse) con.SendRequest(request);
<块引用>
[DirectoryOperationException: 超出大小限制]System.DirectoryServices.Protocols.LdapConnection.ConstructResponse(Int32
messageId、LdapOperation 操作、ResultAll resultType、TimeSpanrequestTimeOut, 布尔异常OnTimeOut) +2385System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest请求,时间跨度请求超时)+499System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest请求)+50UserSearchProvider.ADUserSearchProvider.QueryStore(UserSearchCriteriaCollectionc:UsersstemarieDocumentsVisual 中的标准、操作1 logMessage)工作室2012ProjectsIdealink.ModulesUserSearchProviderUserSearchProviderADUserSearchProvider.cs:298UserSearchProvider.UserSearchProvider.QueryAndSort(UserSearchCriteriaCollectionc:UsersstemarieDocumentsVisual 中的标准、操作
1 logMessage)工作室2012ProjectsIdealink.ModulesUserSearchProviderUserSearchProviderUserSearchProvider.cs:77UserSearchProvider.UserSearchProvider.Search(UserSearchCriteriaCollectionc:UsersstemarieDocumentsVisual 中的标准、操作1 logMessage)工作室2012ProjectsIdealink.ModulesUserSearchProviderUserSearchProviderUserSearchProvider.cs:33UserSearchProvider.UserSearchService.Search(UserSearchCriteriaCollectionc:UsersstemarieDocumentsVisual 中的标准、操作
1 logMessage)工作室2012ProjectsIdealink.ModulesUserSearchProviderUserSearchProviderUserSearchService.cs:44c:UsersstemarieDocumentsVisual Studio 中的 UserSearchProviderTest._Default.Page_Load(Object sender, EventArgs e)2012ProjectsIdealink.ModulesUserSearchProviderUserSearchProviderTestDefault.aspx.cs:28
让我困惑的部分是我们确实指定了最大大小限制,我们不希望超过 100 个条目 - 我们想要限制它.但是,即使我们将 SizeLimit 指定为 1,该库也会始终抛出错误.
是否有人对此问题有任何见解/建议?我们非常接近让这项工作正常进行,只需要解决最后一个问题.
事实证明,这是可行的:
试试{response = (SearchResponse)con.SendRequest(request);返回 response.Entries.Cast().Select(entry => entry.Attributes).Select(x => GetADUserSearchItemFromADProperties(x, logMessage)).Where(user => user.HasName).Cast<IUserSearchItem>();}捕捉(DirectoryOperationException 前){response = (SearchResponse) ex.Response;返回 response.Entries.Cast().Select(entry => entry.Attributes).Select(x => GetADUserSearchItemFromADProperties(x, logMessage)).Where(user => user.HasName).Cast<IUserSearchItem>();}
MSDN 文档指出您会获得 DirectoryResponse 类作为 DirectoryOperationException.Response 属性.但是,您可以将此属性转换为 SearchResponse键入,然后使用 SearchResponse.Entries 属性获取您在达到指定 SizeLimit 之前收到的条目.
我已经尝试过了,我得到了预期的结果,我只是有点沮丧,我必须处理异常才能执行操作.
Because of the fact that we are required to connect to an LDAP server using LDAPS we must use LdapConnection instead of DirectoryEntry.
Here is the source code:
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:
The size limit was exceeded
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.
Exception Details: System.DirectoryServices.Protocols.DirectoryOperationException: The size limit was exceeded
Source Error:
response = (SearchResponse) con.SendRequest(request);
[DirectoryOperationException: The size limit was exceeded] System.DirectoryServices.Protocols.LdapConnection.ConstructResponse(Int32
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, Action
1 logMessage) in c:UsersstemarieDocumentsVisual Studio 2012ProjectsIdealink.ModulesUserSearchProviderUserSearchProviderADUserSearchProvider.cs:298 UserSearchProvider.UserSearchProvider.QueryAndSort(UserSearchCriteriaCollection criterias, Action
1 logMessage) in c:UsersstemarieDocumentsVisual Studio 2012ProjectsIdealink.ModulesUserSearchProviderUserSearchProviderUserSearchProvider.cs:77 UserSearchProvider.UserSearchProvider.Search(UserSearchCriteriaCollection criterias, Action1 logMessage) in c:UsersstemarieDocumentsVisual Studio 2012ProjectsIdealink.ModulesUserSearchProviderUserSearchProviderUserSearchProvider.cs:33 UserSearchProvider.UserSearchService.Search(UserSearchCriteriaCollection criterias, Action
1 logMessage) in c:UsersstemarieDocumentsVisual Studio 2012ProjectsIdealink.ModulesUserSearchProviderUserSearchProviderUserSearchService.cs:44 UserSearchProviderTest._Default.Page_Load(Object sender, EventArgs e) in c:UsersstemarieDocumentsVisual Studio 2012ProjectsIdealink.ModulesUserSearchProviderUserSearchProviderTestDefault.aspx.cs:28
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.
As it turns out, this works:
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>();
}
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 为“超出大小限制"引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!