此链接 http://msdn.microsoft.com/en-us/library/aa772153(VS.85).aspx 说:
您最多可以在单个 LDAP 连接上注册五个通知请求.您必须有一个专用线程来等待通知并快速处理它们.当您调用 ldap_search_ext 函数注册通知请求时,该函数会返回标识该请求的消息标识符.然后使用 ldap_result 函数等待更改通知.发生更改时,服务器会向您发送一条 LDAP 消息,其中包含生成通知的通知请求的消息标识符.这会导致 ldap_result 函数返回标识更改对象的搜索结果.
You can register up to five notification requests on a single LDAP connection. You must have a dedicated thread that waits for the notifications and processes them quickly. When you call the ldap_search_ext function to register a notification request, the function returns a message identifier that identifies that request. You then use the ldap_result function to wait for change notifications. When a change occurs, the server sends you an LDAP message that contains the message identifier for the notification request that generated the notification. This causes the ldap_result function to return with search results that identify the object that changed.
我在 .NET 文档中找不到类似的行为.如果有人知道如何在 C# 中做到这一点,我将不胜感激.我希望查看系统中所有用户的属性何时发生更改,以便我可以根据更改的内容执行自定义操作.
I cannot find a similar behavior looking through the .NET documentation. If anyone knows how to do this in C# I'd be very grateful to know. I'm looking to see when attributes change on all the users in the system so I can perform custom actions depending on what changed.
我浏览了 stackoverflow 和其他来源但没有运气.
I've looked through stackoverflow and other sources with no luck.
谢谢.
我不确定它是否满足您的需求,但请查看 http://dunnry.com/blog/ImplementingChangeNotificationsInNET.aspx
I'm not sure it does what you need, but have a look at http://dunnry.com/blog/ImplementingChangeNotificationsInNET.aspx
从文章中添加文本和代码:
Added text and code from the article:
有三种方法可以确定 Active Directory(或 ADAM)中发生的变化. 这些已经在 MSDN 的适当标题更改跟踪技术概述". 总结:
There are three ways of figuring out things that have changed in Active Directory (or ADAM). These have been documented for some time over at MSDN in the aptly titled "Overview of Change Tracking Techniques". In summary:
在大多数情况下,我发现 DirSync 几乎在所有情况下都适合我. 我从来没有费心去尝试任何其他技术. 但是,一位读者询问是否有办法在 .NET 中执行更改通知. 我认为使用 SDS.P 是可能的,但从未尝试过. 事实证明,这是可能的,而且实际上并不难做到.
For the most part, I have found that DirSync has fit the bill for me in virtually every situation. I never bothered to try any of the other techniques. However, a reader asked if there was a way to do the change notifications in .NET. I figured it was possible using SDS.P, but had never tried it. Turns out, it is possible and actually not too hard to do.
我写这篇文章的第一个想法是使用 示例代码MSDN(并从选项 #3 中引用)并将其简单地转换为 System.DirectoryServices.Protocols. 结果证明这是一个死胡同. 您在 SDS.P 中执行此操作的方式和示例代码的工作方式大不相同,以至于没有任何帮助. 这是我想出的解决方案:
My first thought on writing this was to use the sample code found on MSDN (and referenced from option #3) and simply convert this to System.DirectoryServices.Protocols. This turned out to be a dead end. The way you do it in SDS.P and the way the sample code works are different enough that it is of no help. Here is the solution I came up with:
public class ChangeNotifier : IDisposable
{
LdapConnection _connection;
HashSet<IAsyncResult> _results = new HashSet<IAsyncResult>();
public ChangeNotifier(LdapConnection connection)
{
_connection = connection;
_connection.AutoBind = true;
}
public void Register(string dn, SearchScope scope)
{
SearchRequest request = new SearchRequest(
dn, //root the search here
"(objectClass=*)", //very inclusive
scope, //any scope works
null //we are interested in all attributes
);
//register our search
request.Controls.Add(new DirectoryNotificationControl());
//we will send this async and register our callback
//note how we would like to have partial results
IAsyncResult result = _connection.BeginSendRequest(
request,
TimeSpan.FromDays(1), //set timeout to a day...
PartialResultProcessing.ReturnPartialResultsAndNotifyCallback,
Notify,
request);
//store the hash for disposal later
_results.Add(result);
}
private void Notify(IAsyncResult result)
{
//since our search is long running, we don't want to use EndSendRequest
PartialResultsCollection prc = _connection.GetPartialResults(result);
foreach (SearchResultEntry entry in prc)
{
OnObjectChanged(new ObjectChangedEventArgs(entry));
}
}
private void OnObjectChanged(ObjectChangedEventArgs args)
{
if (ObjectChanged != null)
{
ObjectChanged(this, args);
}
}
public event EventHandler<ObjectChangedEventArgs> ObjectChanged;
#region IDisposable Members
public void Dispose()
{
foreach (var result in _results)
{
//end each async search
_connection.Abort(result);
}
}
#endregion
}
public class ObjectChangedEventArgs : EventArgs
{
public ObjectChangedEventArgs(SearchResultEntry entry)
{
Result = entry;
}
public SearchResultEntry Result { get; set;}
}
这是一个相对简单的类,可用于注册搜索.诀窍是在回调方法中使用 GetPartialResults 方法来仅获取刚刚发生的更改.我还包含了非常简化的 EventArgs 类,我用来将结果传回.请注意,我在这里没有做任何关于线程的事情,也没有任何错误处理(这只是一个示例).你可以像这样使用这个类:
It is a relatively simple class that you can use to register searches. The trick is using the GetPartialResults method in the callback method to get only the change that has just occurred. I have also included the very simplified EventArgs class I am using to pass results back. Note, I am not doing anything about threading here and I don't have any error handling (this is just a sample). You can consume this class like so:
static void Main(string[] args)
{
using (LdapConnection connect = CreateConnection("localhost"))
{
using (ChangeNotifier notifier = new ChangeNotifier(connect))
{
//register some objects for notifications (limit 5)
notifier.Register("dc=dunnry,dc=net", SearchScope.OneLevel);
notifier.Register("cn=testuser1,ou=users,dc=dunnry,dc=net", SearchScope.Base);
notifier.ObjectChanged += new EventHandler<ObjectChangedEventArgs>(notifier_ObjectChanged);
Console.WriteLine("Waiting for changes...");
Console.WriteLine();
Console.ReadLine();
}
}
}
static void notifier_ObjectChanged(object sender, ObjectChangedEventArgs e)
{
Console.WriteLine(e.Result.DistinguishedName);
foreach (string attrib in e.Result.Attributes.AttributeNames)
{
foreach (var item in e.Result.Attributes[attrib].GetValues(typeof(string)))
{
Console.WriteLine(" {0}: {1}", attrib, item);
}
}
Console.WriteLine();
Console.WriteLine("====================");
Console.WriteLine();
}
这篇关于使用 C# 向 Active Directory 注册更改通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!