public static DsNameResultItem CrackNames(DsNameFormat formatOffered, DsNameFormat formatDesired, string name, string dnsDomainName = null, int referralLevel = 0) { IntPtr hds = IntPtr.Zero; try { int result = NativeMethods.DsBind(null, dnsDomainName, out hds); if (result != 0) { throw new DirectoryException("DsBind failed", new Win32Exception(result)); } DsNameResultItem nameResult = NativeMethods.CrackNames(hds, DsNameFlags.DS_NAME_FLAG_TRUST_REFERRAL, formatOffered, formatDesired, name); switch (nameResult.Status) { case DsNameError.None: return(nameResult); case DsNameError.NoMapping: throw new NameMappingException($"The object name {name} was found in the global catalog, but could not be mapped to a DN. DsCrackNames returned NO_MAPPING"); case DsNameError.TrustReferral: case DsNameError.DomainOnly: if (!string.IsNullOrWhiteSpace(nameResult.Domain)) { if (referralLevel < NativeMethods.DirectoryReferralLimit) { return(NativeMethods.CrackNames(formatOffered, formatDesired, name, nameResult.Domain, ++referralLevel)); } throw new ReferralLimitExceededException("The referral limit exceeded the maximum configured value"); } throw new ReferralFailedException($"A referral to the object name {name} was received from the global catalog, but no referral information was provided. DsNameError: {nameResult.Status}"); case DsNameError.NotFound: throw new ObjectNotFoundException($"The object name {name} was not found in the global catalog"); case DsNameError.NotUnique: throw new AmbiguousNameException($"There was more than one object with the name {name} in the global catalog"); case DsNameError.Resolving: throw new NameMappingException($"The object name {name} was not able to be resolved in the global catalog. DsCrackNames returned RESOLVING"); case DsNameError.NoSyntacticalMapping: throw new NameMappingException($"DsCrackNames unexpectedly returned DS_NAME_ERROR_NO_SYNTACTICAL_MAPPING for name {name}"); default: throw new NameMappingException($"An unexpected status was returned from DsCrackNames {nameResult.Status}"); } } finally { if (hds != IntPtr.Zero) { NativeMethods.DsUnBind(hds); } } }
private static DsNameResultItem[] CrackNames( SafeDsHandle hDS, DsNameFlags flags, DsNameFormat formatOffered, DsNameFormat formatDesired, string[] names) { PscxArgumentException.ThrowIfIsNullOrEmpty(names); IntPtr ptr = IntPtr.Zero; try { NativeMethods.EnforceSuccess(NativeMethods.DsCrackNames( hDS, flags, formatOffered, formatDesired, names.Length, names, out ptr)); DsNameResult result = Utils.PtrToStructure <DsNameResult>(ptr); IEnumerable <DsNameResultItem> items = Utils.ReadNativeArray <DsNameResultItem>(result.rItems, result.cItems); return(new List <DsNameResultItem>(items).ToArray()); } finally { NativeMethods.DsFreeNameResult(ptr); } }
public static extern int DsCrackNames( SafeDsHandle hDS, DsNameFlags flags, DsNameFormat formatOffered, DsNameFormat formatDesired, int cNames, string[] rpNames, out IntPtr ppResult //pointer to pointer of DS_NAME_RESULT );
private DirectoryEntry GetDirectoryEntry(string nameToFind, DsNameFormat nameFormat) { return(this.discoveryServices.FindDcAndExecuteWithRetry(dc => { var result = NativeMethods.CrackNames(nameFormat, DsNameFormat.DistinguishedName, nameToFind, dc, null); return this.discoveryServices.FindDcAndExecuteWithRetry(result.Domain, dc2 => { var de = new DirectoryEntry($"LDAP://{dc2}/{result.Name}"); _ = de.Guid; return de; }); })); }
private static DsNameResultItem[] CrackNames(IntPtr hds, DsNameFlags flags, DsNameFormat formatOffered, DsNameFormat formatDesired, string[] namesToCrack) { IntPtr pDsNameResult = IntPtr.Zero; DsNameResultItem[] resultItems; try { uint namesToCrackCount = (uint)namesToCrack.Length; int result = NativeMethods.DsCrackNames(hds, flags, formatOffered, formatDesired, namesToCrackCount, namesToCrack, out pDsNameResult); if (result != 0) { throw new DirectoryException("DsCrackNames failed", new Win32Exception(result)); } DsNameResult dsNameResult = (DsNameResult)Marshal.PtrToStructure(pDsNameResult, typeof(DsNameResult)); if (dsNameResult.cItems == 0) { throw new DirectoryException("DsCrackNames returned an unexpected result"); } resultItems = new DsNameResultItem[dsNameResult.cItems]; IntPtr pItem = dsNameResult.rItems; for (int idx = 0; idx < dsNameResult.cItems; idx++) { resultItems[idx] = (DsNameResultItem)Marshal.PtrToStructure(pItem, typeof(DsNameResultItem)); pItem = IntPtr.Add(pItem, Marshal.SizeOf(resultItems[idx])); } } finally { if (pDsNameResult != IntPtr.Zero) { NativeMethods.DsFreeNameResult(pDsNameResult); } } return(resultItems); }
private static DsNameResultItem CrackNames(IntPtr hds, DsNameFlags flags, DsNameFormat formatOffered, DsNameFormat formatDesired, string name) { DsNameResultItem[] resultItems = NativeMethods.CrackNames(hds, flags, formatOffered, formatDesired, new[] { name }); return(resultItems[0]); }
private static extern int DsCrackNames(IntPtr hds, DsNameFlags flags, DsNameFormat formatOffered, DsNameFormat formatDesired, uint cNames, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPTStr, SizeParamIndex = 4)] string[] rpNames, out IntPtr ppResult);
public static DirectoryEntry GetDirectoryEntry(string nameToFind, DsNameFormat nameFormat, string server) { var result = CrackNames(nameFormat, DsNameFormat.DistinguishedName, nameToFind, server); return(new DirectoryEntry($"LDAP://{result.Domain}/{result.Name}")); }
public static DirectoryEntry GetDirectoryEntry(string nameToFind, DsNameFormat nameFormat) { return(GetDirectoryEntry(nameToFind, nameFormat, null)); }
public static string GetDn(string nameToFind, DsNameFormat nameFormat, string server) { var result = CrackNames(nameFormat, DsNameFormat.DistinguishedName, nameToFind, server); return(result.Name); }
public static string GetDn(string nameToFind, DsNameFormat nameFormat) { return(GetDn(nameToFind, nameFormat, null)); }
public string TranslateName(string name, DsNameFormat nameFormat, DsNameFormat requiredFormat, string dnsDomainName) { return(this.discoveryServices.FindDcAndExecuteWithRetry(dnsDomainName, dc => NativeMethods.CrackNames(nameFormat, requiredFormat, name, dc, dnsDomainName).Name)); }
public string TranslateName(string name, DsNameFormat nameFormat, DsNameFormat requiredFormat) { return(this.TranslateName(name, nameFormat, requiredFormat, null)); }
public string TranslateName(string name, DsNameFormat nameFormat, DsNameFormat requiredFormat, string dnsDomainName) { return(NativeMethods.CrackNames(nameFormat, requiredFormat, name, dnsDomainName).Name); }
private static DsNameResultItem[] CrackNames( SafeDsHandle hDS, DsNameFlags flags, DsNameFormat formatOffered, DsNameFormat formatDesired, string[] names) { PscxArgumentException.ThrowIfIsNullOrEmpty(names); IntPtr ptr = IntPtr.Zero; try { NativeMethods.EnforceSuccess(NativeMethods.DsCrackNames( hDS, flags, formatOffered, formatDesired, names.Length, names, out ptr)); DsNameResult result = Utils.PtrToStructure<DsNameResult>(ptr); IEnumerable<DsNameResultItem> items = Utils.ReadNativeArray<DsNameResultItem>(result.rItems, result.cItems); return new List<DsNameResultItem>(items).ToArray(); } finally { NativeMethods.DsFreeNameResult(ptr); } }