public static Dictionary <string, List <string> > Parse(string distingishedName) { var result = new Dictionary <string, List <string> >(StringComparer.CurrentCultureIgnoreCase); var distinguishedNamePtr = IntPtr.Zero; try { distinguishedNamePtr = Marshal.StringToCoTaskMemUni(distingishedName); //We need to copy the IntPtr. //The copy is necessary because DsGetRdnW modifies the pointer to advance it. We need to keep //The original so we can free it later, otherwise we'll leak memory. var distinguishedNamePtrCopy = distinguishedNamePtr; uint pcDN = (uint)distingishedName.Length, pcKey, pcVal; IntPtr ppKey, ppVal; while (pcDN != 0 && Ntdsapi.DsGetRdnW(ref distinguishedNamePtrCopy, ref pcDN, out ppKey, out pcKey, out ppVal, out pcVal) == 0) { if (pcKey == 0 || pcVal == 0) { continue; } var key = Marshal.PtrToStringUni(ppKey, (int)pcKey); var value = Marshal.PtrToStringUni(ppVal, (int)pcVal); if (result.ContainsKey(key)) { result[key].Add(value); } else { result.Add(key, new List <string> { value }); } if (pcDN == 0) { break; } } return(result); } finally { Marshal.FreeCoTaskMem(distinguishedNamePtr); } }
/// <summary> /// Converts an array of directory service object names from one format to another. Name conversion enables client applications to map between the multiple names used to identify various directory service objects. /// </summary> /// <param name="names">The names to convert.</param> /// <param name="flags">Values used to determine how the name syntax will be cracked.</param> /// <param name="formatOffered">Format of the input names.</param> /// <param name="formatDesired">Desired format for the output names.</param> /// <returns>An array of DS_NAME_RESULT_ITEM structures. Each element of this array represents a single converted name.</returns> public DS_NAME_RESULT_ITEM[] CrackNames(string[] names = null, DS_NAME_FLAGS flags = DS_NAME_FLAGS.DS_NAME_NO_FLAGS, DS_NAME_FORMAT formatOffered = DS_NAME_FORMAT.DS_UNKNOWN_NAME, DS_NAME_FORMAT formatDesired = DS_NAME_FORMAT.DS_USER_PRINCIPAL_NAME) { IntPtr pResult; uint err = Ntdsapi.DsCrackNames(handle, flags, formatOffered, formatDesired, (uint)(names?.Length ?? 0), names, out pResult); if (err != (uint)DS_NAME_ERROR.DS_NAME_NO_ERROR) { throw new System.ComponentModel.Win32Exception((int)err); } try { // Next convert the returned structure to managed environment DS_NAME_RESULT Result = (DS_NAME_RESULT)Marshal.PtrToStructure(pResult, typeof(DS_NAME_RESULT)); return(Result.Items); } finally { Ntdsapi.DsFreeNameResult(pResult); } }
/// <summary> /// Initializes a new instance of the <see cref="DomainService"/> class. /// </summary> /// <param name="domainControllerName">Name of the domain controller.</param> /// <param name="dnsDomainName">Name of the DNS domain.</param> /// <exception cref="System.ComponentModel.Win32Exception"></exception> public DomainService(string domainControllerName = null, string dnsDomainName = null) { Ntdsapi.DsBind(domainControllerName, dnsDomainName, out handle); }
public void Dispose() { uint ret = Ntdsapi.DsUnBind(ref handle); System.Diagnostics.Debug.WriteLineIf(ret != 0, "Error unbinding :\t" + ret.ToString()); }