[WebMethod]
public void Synchronize ( string DomainPath, string Filter, string EntryPath)
{
DirectoryEntry de = new DirectoryEntry (DomainPath);
using (SqlConnection conn = new SqlConnection (Globals.ConnectionString))
{
conn.Open ();
SqlCommand command = conn.CreateCommand ();
command.CommandText = "INSERT INTO SyncTable (Snapshot, OU) VALUES (@Snapshot, @OU, @ExchangeServer)" ;
command.Parameters.AddWithValue ( "@Snapshot" , GetSyncData (de, filter));
command.Parameters.AddWithValue ( "@OU" , EntryPath);
command.ExecuteNonQuery ();
conn.Close ();
}
}
public byte [] DirectorySync (DirectoryEntry DomainDE, string Filter)
{
ADInit.Init ();
try
{
using (DirectorySearcher srch = new DirectorySearcher (DomainDE, Filter))
{
srch.SearchScope = SearchScope.Base;
srch.DirectorySynchronization = new DirectorySynchronization ();
foreach (SearchResult se in srch.FindAll ())
{
}
MemoryStream ms = new MemoryStream ();
BinaryFormatter bFormat = new BinaryFormatter ();
bFormat.Serialize (ms, srch.DirectorySynchronization.GetDirectorySynchronizationCookie ());
ms.Close ();
return ms.GetBuffer ();
}
}
catch (Exception Ex)
{
throw Ex;
}
}
[WebMethod]
public bool GetRegionEntry ( string Domain, string Filter, string EntryPath)
{
DirectoryEntry de = new DirectoryEntry (Domain);
RegionInfo _regionInfo = regionEntry.GetEntry ();
using (SqlConnection conn = new SqlConnection (Globals.ConnectionString))
{
conn.Open ();
SqlCommand command = conn.CreateCommand ();
command.CommandText = "SELECT * FROM SyncTable WHERE OU = @OU" ;
command.Parameters.AddWithValue ( "@OU" , EntryPath);
SqlDataReader reader = command.ExecuteReader ();
if (reader.Read ())
{
return regionEntry.GetSyncDelta (de, Filter, ( byte []) reader [ "Snapshot" ]);
}
else
{
throw new Exception (“Can't read Sync Cookie from Database!”);
}
conn.Close ();
}
return _regionInfo;
}
/// public Dictionary <string, object> GetSyncDelta (DirectoryEntry DomainDE, string Filter, byte [] _cookie)
public bool GetSyncDelta (DirectoryEntry DomainDE, string Filter, byte [] _cookie)
{
Dictionary < string , object > _delta = new Dictionary < string , object > ();
BinaryFormatter bf = new BinaryFormatter ();
byte [] cookie = ( byte []) bf.Deserialize ( new MemoryStream (_cookie));
DirectorySynchronization dirSync = new DirectorySynchronization (cookie);
DirectorySearcher srch = new DirectorySearcher (DomainDE, Filter);
srch.DirectorySynchronization = dirSync;
foreach (SearchResult sr in srch.FindAll ())
{
foreach ( string attrName in sr.Properties.PropertyNames)
{
_delta.Add (attrName, sr.Properties [attrName]);
}
return true ;
}
return false ;
// return _delta;
}
ALTER TRIGGER [SyncTableTrg]
ON [dbo]. [SyncTable]
AFTER INSERT , UPDATE
AS
BEGIN
INSERT INTO SyncTableLog (OU, Snapshot)
SELECT OU, Snapshot FROM inserted
END
Source: https://habr.com/ru/post/29180/
All Articles