diff --git a/LdapForNet/LdapSearchExtensions.cs b/LdapForNet/LdapSearchExtensions.cs index 94e9b63..f789e59 100644 --- a/LdapForNet/LdapSearchExtensions.cs +++ b/LdapForNet/LdapSearchExtensions.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using LdapForNet.Utils; using static LdapForNet.Native.Native; @@ -8,40 +9,52 @@ namespace LdapForNet { public static class LdapSearchExtensions { - public static IList SearchByCn(this ILdapConnection connection, string @base, string cn, - LdapSearchScope scope = LdapSearchScope.LDAP_SCOPE_SUBTREE) + public static LdapEntry GetRootDse(this ILdapConnection connection) { - return connection.Search(@base, $"(cn={cn})", scope: scope); + return connection.Search( + null, + "(objectclass=*)", + new[] + { + "*", "+", "serverName", "dNSHostName", "dsServiceName", "currentTime", "defaultNamingContext", + "rootDomainNamingContext", "configurationNamingContext", "schemaNamingContext", "subschemaSubentry", + "namingContexts", "supportedLDAPVersion", "supportedCapabilities", "supportedControl", + "supportedLDAPPolicies", "supportedSaslMechanisms", "supportedExtension", "vendorName", "vendorVersion", + "domainControllerFunctionality", "domainFunctionality", "forestFunctionality", "isSynchronized", "objectClass" + }, + LdapSearchScope.LDAP_SCOPE_BASE) + .First(); } - public static LdapEntry GetRootDse(this ILdapConnection connection) + public static async Task GetRootDseAsync(this ILdapConnection connection, + CancellationToken token = default) { - var result = connection.Search(null, "(objectclass=*)", - new[] - { - "namingContexts", "subschemaSubentry", "supportedLDAPVersion", "supportedSASLMechanisms", - "supportedExtension", "supportedControl", "supportedFeatures", "vendorName", "vendorVersion" - }, LdapSearchScope.LDAP_SCOPE_BASE) - .FirstOrDefault(); - if (result == null) - { - return null; - } - - var rootDse = connection.Search(null, "(objectclass=*)", scope: LdapSearchScope.LDAP_SCOPE_BASE).First(); - foreach (var attribute in rootDse.DirectoryAttributes) - { - result.DirectoryAttributes.Remove(attribute.Name); - result.DirectoryAttributes.Add(attribute); - } + return (await connection.SearchAsync( + null, + "(objectclass=*)", + new[] + { + "*", "+", "serverName", "dNSHostName", "dsServiceName", "currentTime", "defaultNamingContext", + "rootDomainNamingContext", "configurationNamingContext", "schemaNamingContext", "subschemaSubentry", + "namingContexts", "supportedLDAPVersion", "supportedCapabilities", "supportedControl", + "supportedLDAPPolicies", "supportedSaslMechanisms", "supportedExtension", "vendorName", "vendorVersion", + "domainControllerFunctionality", "domainFunctionality", "forestFunctionality", "isSynchronized", "objectClass" + }, + LdapSearchScope.LDAP_SCOPE_BASE, + token)) + .First(); + } - return result; + public static IList SearchByCn(this ILdapConnection connection, string @base, string cn, + LdapSearchScope scope = LdapSearchScope.LDAP_SCOPE_SUBTREE) + { + return connection.Search(@base, $"(cn={cn})", scope: scope); } public static async Task> SearchByCnAsync(this ILdapConnection connection, string @base, - string cn, LdapSearchScope scope = LdapSearchScope.LDAP_SCOPE_SUBTREE) + string cn, LdapSearchScope scope = LdapSearchScope.LDAP_SCOPE_SUBTREE, CancellationToken token = default) { - return await connection.SearchAsync(@base, $"(cn={cn})", scope: scope); + return await connection.SearchAsync(@base, $"(cn={cn})", scope: scope, token: token); } public static IList SearchByCn(this ILdapConnection connection, string cn) @@ -49,9 +62,10 @@ public static IList SearchByCn(this ILdapConnection connection, strin return connection.SearchByCn(LdapUtils.GetDnFromHostname(), cn); } - public static async Task> SearchByCnAsync(this ILdapConnection connection, string cn) + public static async Task> SearchByCnAsync(this ILdapConnection connection, string cn, + CancellationToken token = default) { - return await connection.SearchByCnAsync(LdapUtils.GetDnFromHostname(), cn); + return await connection.SearchByCnAsync(LdapUtils.GetDnFromHostname(), cn, token: token); } public static IList SearchBySid(this ILdapConnection connection, string @base, string sid, @@ -62,10 +76,10 @@ public static IList SearchBySid(this ILdapConnection connection, stri } public static async Task> SearchBySidAsync(this ILdapConnection connection, string @base, - string sid, LdapSearchScope scope = LdapSearchScope.LDAP_SCOPE_SUBTREE) + string sid, LdapSearchScope scope = LdapSearchScope.LDAP_SCOPE_SUBTREE, CancellationToken token = default) { var hex = HexEscaper.Escape(LdapSidConverter.ConvertToHex(sid)); - return await connection.SearchAsync(@base, $"(objectSID={hex})", scope: scope); + return await connection.SearchAsync(@base, $"(objectSID={hex})", scope: scope, token: token); } public static IList SearchBySid(this ILdapConnection connection, string sid) @@ -73,9 +87,10 @@ public static IList SearchBySid(this ILdapConnection connection, stri return connection.SearchBySid(LdapUtils.GetDnFromHostname(), sid); } - public static async Task> SearchBySidAsync(this ILdapConnection connection, string sid) + public static async Task> SearchBySidAsync(this ILdapConnection connection, string sid, + CancellationToken token = default) { - return await connection.SearchBySidAsync(LdapUtils.GetDnFromHostname(), sid); + return await connection.SearchBySidAsync(LdapUtils.GetDnFromHostname(), sid, token: token); } } } \ No newline at end of file