Esempio n. 1
0
 public static string DistinguishedNameFromCanonicalName(string canonicalName)
 {
     return(NativeHelpers.DistinguishedNameFromCanonicalName(canonicalName, null));
 }
Esempio n. 2
0
        private static bool TryDsCrackNames(string input, ExtendedNameFormat formatOffered, ExtendedNameFormat formatDesired, ADServerSettings serverSettings, out string result, out uint err)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            if (formatOffered != ExtendedNameFormat.NameCanonical && formatOffered != ExtendedNameFormat.NameFullyQualifiedDN)
            {
                throw new ArgumentException(DirectoryStrings.ExArgumentException("formatOffered", formatOffered.ToString()), "formatOffered");
            }
            if (formatDesired != ExtendedNameFormat.NameCanonical && formatDesired != ExtendedNameFormat.NameFullyQualifiedDN)
            {
                throw new ArgumentException(DirectoryStrings.ExArgumentException("formatDesired", formatDesired.ToString()), "formatDesired");
            }
            result = null;
            err    = 0U;
            SafeDsNameResultHandle safeDsNameResultHandle = null;
            SafeDsBindHandle       safeDsBindHandle       = null;

            NativeMethods.DsNameFlags dsNameFlags = NativeMethods.DsNameFlags.SyntacticalOnly;
            bool result2;

            try
            {
                if (formatOffered == ExtendedNameFormat.NameCanonical)
                {
                    string[] array = input.Split(new char[]
                    {
                        '/'
                    });
                    if (array.Length == 0)
                    {
                        return(false);
                    }
                    if (array.Length > 1 && array[1].Length > 0)
                    {
                        dsNameFlags = NativeMethods.DsNameFlags.NoFlags;
                        string text = array[0];
                        ExTraceGlobals.ADTopologyTracer.TraceDebug <string>(0L, "Calling DsBind for domain {0}", text ?? "<null>");
                        if (text.Length == 0)
                        {
                            return(false);
                        }
                        string domainControllerName = null;
                        string distinguishedName    = NativeHelpers.DistinguishedNameFromCanonicalName(text);
                        if (serverSettings == null)
                        {
                            serverSettings = ADSessionSettings.ExternalServerSettings;
                        }
                        if (serverSettings != null)
                        {
                            domainControllerName = serverSettings.GetPreferredDC(new ADObjectId(distinguishedName));
                        }
                        err = NativeMethods.DsBind(domainControllerName, text, out safeDsBindHandle);
                        if (err != 0U)
                        {
                            return(false);
                        }
                    }
                }
                if (safeDsBindHandle == null)
                {
                    safeDsBindHandle = new SafeDsBindHandle();
                }
                ExTraceGlobals.ADTopologyTracer.TraceDebug(0L, "Calling DsCrackNames with input={0}, formatOffered={1}, formatDesired={2}, flags={3}", new object[]
                {
                    input,
                    formatOffered,
                    formatDesired,
                    dsNameFlags
                });
                err = NativeMethods.DsCrackNames(safeDsBindHandle, dsNameFlags, formatOffered, formatDesired, 1U, new string[]
                {
                    input
                }, out safeDsNameResultHandle);
                if (err != 0U)
                {
                    result2 = false;
                }
                else
                {
                    NativeMethods.DsNameResult dsNameResult = new NativeMethods.DsNameResult();
                    Marshal.PtrToStructure(safeDsNameResultHandle.DangerousGetHandle(), dsNameResult);
                    uint cItems = dsNameResult.cItems;
                    if (cItems < 1U)
                    {
                        result2 = false;
                    }
                    else
                    {
                        NativeMethods.DsNameResultItem dsNameResultItem = new NativeMethods.DsNameResultItem();
                        Marshal.PtrToStructure(dsNameResult.rItems, dsNameResultItem);
                        if (dsNameResultItem.status != 0)
                        {
                            err     = (uint)dsNameResultItem.status;
                            result2 = false;
                        }
                        else if (dsNameResultItem.name == null)
                        {
                            result2 = false;
                        }
                        else
                        {
                            result  = dsNameResultItem.name;
                            result2 = true;
                        }
                    }
                }
            }
            finally
            {
                if (safeDsBindHandle != null)
                {
                    safeDsBindHandle.Dispose();
                }
                if (safeDsNameResultHandle != null)
                {
                    safeDsNameResultHandle.Dispose();
                }
            }
            return(result2);
        }