虽然异步请求通过BeginSendRequest和EndSendRequest执行LDAP操作非常简单,但我无法确定如何异步执行绑定过程。
是否有可能与SDS.P的LdapConnection异步绑定?
发布于 2022-04-15 22:20:43
正在寻找进行异步LDAP操作,并发现了这个未回答的问题。最后我自己弄明白了:
假设您还想在绑定之后异步发送/接收LDAP请求(不确定为什么还要绑定),您可以在LdapConnection上使用LdapConnection属性并在构造函数中预先指定凭据,通过使用BeginSendRequest()/EndSendRequest()并让它以异步方式在内部处理绑定,从而实现“异步绑定”。
using System.DirectoryServices.Protocols;
using System.Net;
using System.Threading.Tasks;
// ...
using (var connection =
new LdapConnection(
new LdapDirectoryIdentifier("fqdn.example.com", true, false),
new NetworkCredential("someuser", "somepassword"),
AuthType.Basic))
{
// The Answer...
connection.AutoBind = true;
var searchResult = await Task.Factory.FromAsync(
connection.BeginSendRequest,
connection.EndSendRequest,
new SearchRequest(
"DC=example,DC=com",
"(objectClass=user)",
SearchScope.Subtree,
"distinguishedname"),
PartialResultProcessing.NoPartialResultSupport,
null);
// Profit
}为了简洁起见,我将LdapConnection上的旧异步编程模型方法(APM)放入了为基于任务的异步模式(TAP)提供的方便的包装方法中,这样就可以在现代.Net项目中像预期的那样简单地等待它。请参阅:https://learn.microsoft.com/en-us/dotnet/standard/asynchronous-programming-patterns/interop-with-other-asynchronous-patterns-and-types
并没有真正准确地回答这个问题,但它确实表明,您并不需要明确地绑定自己,这也许就是MS没有费心添加BeginBind()等的原因。希望这有助于下一个疲惫不堪的程序员与LDAP互操作进行斗争。
https://stackoverflow.com/questions/41238998
复制相似问题