Exemplo n.º 1
0
        private bool BindSam(string target, string userName, string password)
        {
            string adsPath = $"WinNT://{_serverName},computer";
            Guid   g       = new Guid("fd8256d0-fd15-11ce-abc4-02608c9e7553"); // IID_IUnknown
            object value   = null;
            // always attempt secure auth..
            int    authenticationType = 1;
            object unmanagedResult    = null;

            try
            {
                if (Thread.CurrentThread.GetApartmentState() == ApartmentState.Unknown)
                {
                    Thread.CurrentThread.SetApartmentState(ApartmentState.MTA);
                }
                // We need the credentials to be in the form <machine>\\<user>
                // if they just passed user then append the machine name here.
                if (null != userName)
                {
                    int index = userName.IndexOf('\\');
                    if (index == -1)
                    {
                        userName = _serverName + "\\" + userName;
                    }
                }

                int hr = UnsafeNativeMethods.ADsOpenObject(adsPath, userName, password, (int)authenticationType, ref g, out value);

                if (hr != 0)
                {
                    if (hr == unchecked ((int)(ExceptionHelper.ERROR_HRESULT_LOGON_FAILURE)))
                    {
                        // This is the invalid credetials case.  We want to return false
                        // instead of throwing an exception
                        return(false);
                    }
                    else
                    {
                        throw ExceptionHelper.GetExceptionFromErrorCode(hr);
                    }
                }

                unmanagedResult = ((UnsafeNativeMethods.IADs)value).Get("name");
            }
            catch (System.Runtime.InteropServices.COMException e)
            {
                if (e.ErrorCode == unchecked ((int)(ExceptionHelper.ERROR_HRESULT_LOGON_FAILURE)))
                {
                    return(false);
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(e);
                }
            }
            finally
            {
                if (value != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(value);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        private bool BindSam(string target, string userName, string password)
        {
            bool          flag;
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("WinNT://");
            stringBuilder.Append(this.serverName);
            stringBuilder.Append(",computer");
            Guid   guid = new Guid("fd8256d0-fd15-11ce-abc4-02608c9e7553");
            object obj  = null;
            int    num  = 1;

            try
            {
                try
                {
                    if (Thread.CurrentThread.GetApartmentState() == ApartmentState.Unknown)
                    {
                        Thread.CurrentThread.SetApartmentState(ApartmentState.MTA);
                    }
                    if (userName != null)
                    {
                        int num1 = userName.IndexOf("\\", StringComparison.Ordinal);
                        if (num1 == -1)
                        {
                            userName = string.Concat(this.serverName, "\\", userName);
                        }
                    }
                    int num2 = UnsafeNativeMethods.ADsOpenObject(stringBuilder.ToString(), userName, password, num, out guid, out obj);
                    if (num2 == 0)
                    {
                        ((UnsafeNativeMethods.IADs)obj).Get("name");
                    }
                    else
                    {
                        if (num2 != ExceptionHelper.ERROR_HRESULT_LOGON_FAILURE)
                        {
                            throw ExceptionHelper.GetExceptionFromErrorCode(num2);
                        }
                        else
                        {
                            flag = false;
                            return(flag);
                        }
                    }
                }
                catch (COMException cOMException1)
                {
                    COMException cOMException = cOMException1;
                    if (cOMException.ErrorCode != ExceptionHelper.ERROR_HRESULT_LOGON_FAILURE)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(cOMException);
                    }
                    else
                    {
                        flag = false;
                        return(flag);
                    }
                }
                return(true);
            }
            finally
            {
                if (obj != null)
                {
                    Marshal.ReleaseComObject(obj);
                }
            }
            return(flag);
        }