Exemple #1
0
        public static bool GetUserNameEx(ExtendedNameFormat nameFormat, StringBuilder userName, ref uint userNameSize)
        {
            string value = null;

            switch (nameFormat)
            {
            case ExtendedNameFormat.NameDisplay:
                value = FoundationStatic.FullUserName;
                break;

            case ExtendedNameFormat.NameUnknown:
            case ExtendedNameFormat.NameFullyQualifiedDN:
            case ExtendedNameFormat.NameSamCompatible:
            case ExtendedNameFormat.NameUniqueId:
            case ExtendedNameFormat.NameCanonical:
            case ExtendedNameFormat.NameUserPrincipal:
            case ExtendedNameFormat.NameCanonicalEx:
            case ExtendedNameFormat.NameServicePrincipal:
            case ExtendedNameFormat.NameDnsDomain:
            default:
                NotImplemented(MethodBase.GetCurrentMethod());
                userNameSize = 0;
                return(false);
            }

            userName.Append(value);
            userNameSize = (uint)value.Length;
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Gets the user name in the specified format. Returns null for
        /// formats that aren't mapped.
        /// </summary>
        public static string?GetUserName(ExtendedNameFormat format)
        {
            return(BufferHelper.BufferInvoke((StringBuffer buffer) =>
            {
                uint size = buffer.CharCapacity;
                while (!Imports.GetUserNameExW(format, buffer, ref size))
                {
                    WindowsError error = Error.GetLastError();
                    switch (error)
                    {
                    case WindowsError.ERROR_NONE_MAPPED:
                        return null;

                    case WindowsError.ERROR_MORE_DATA:
                        buffer.EnsureCharCapacity(size);
                        break;

                    default:
                        throw error.GetException();
                    }
                }

                buffer.Length = size;
                return buffer.ToString();
            }));
        }
Exemple #3
0
        /// <summary>
        /// Return the current user's full display name
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string CurrentUserFullName(ExtendedNameFormat type = ExtendedNameFormat.NameDisplay)
        {
            string CurrentUserFullNameRet = default;
            MemPtr lps;
            int    cb = 10240;

            lps = new MemPtr(10240L);
            lps.ZeroMemory();

            if (GetUserNameEx(type, lps.Handle, ref cb))
            {
                CurrentUserFullNameRet = lps.ToString();
            }
            else
            {
                CurrentUserFullNameRet = null;
            }

            lps.Free();
            return(CurrentUserFullNameRet);
        }
Exemple #4
0
 internal static extern bool GetUserNameEx([In] ExtendedNameFormat nameFormat, StringBuilder nameBuffer, ref uint size);
 internal static extern uint DsCrackNames(SafeDsBindHandle phDS, NativeMethods.DsNameFlags flags, ExtendedNameFormat formatOffered, ExtendedNameFormat formatDesired, uint cNames, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 4)] string[] rpNames, out SafeDsNameResultHandle ppResult);
        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);
        }
Exemple #7
0
 public static extern Boolean8 GetUserNameExW(
     ExtendedNameFormat NameFormat,
     SafeHandle lpNameBuffer,
     ref uint lpnSize);
Exemple #8
0
 private static extern int GetUserNameEx(ExtendedNameFormat nameFormat,
                                         StringBuilder userName, ref int userNameSize);
Exemple #9
0
 static extern bool GetUserNameEx(ExtendedNameFormat NameFormat, IntPtr lpNameBuffer, ref int lpnSize);
Exemple #10
0
 static extern bool GetUserNameEx(ExtendedNameFormat NameFormat, [MarshalAs(UnmanagedType.LPWStr)] string lpNameBuffer, ref int lpnSize);