예제 #1
0
파일: SidInfo.cs 프로젝트: Yikez978/aTree
        // '' <summary>
        // '' Gets account info using an account name through the LookupAccountName function.
        // '' </summary>
        // '' <param name="Account">The name to use as a lookup parameter.</param>
        // '' <param name="System">The system to search for the SID, or blank if the account is not a local account.</param>
        // '' <param name="SID">A reference to a string that will contain the SID.</param>
        // '' <param name="Domain">A reference to a string that will contain the Domain.</param>
        // '' <param name="SIDUse">A reference to an integer that will contain the SID type.</param>
        // '' <returns></returns>
        // '' <remarks></remarks>
        public static int GetAccountInfoByName(string Account, string System, ref string SID, ref string Domain, ref int SIDUse)
        {
            int Result;
            // The buffer sizes can be adjusted if desired, but the return values will probably never exceed 255.
            // So this is a good, safe value.
            string AccountName          = Account;
            IntPtr SIDPointer           = new IntPtr();
            int    SIDPointerBufferSize = 255;
            IntPtr DomainNamePointer    = new IntPtr();
            int    DomainNameBufferSize = 255;
            int    Use = 0;

            // Since we're dealing with unmanaged code, we have to allocate memory for these manually.
            SIDPointer        = Marshal.AllocHGlobal(SIDPointerBufferSize);
            DomainNamePointer = Marshal.AllocHGlobal(DomainNameBufferSize);
            // Calling the function, the return value will contain either 0 on failure, or non-zero on success.
            Result = SIDInfo.LookupAccountName(System, AccountName, SIDPointer, ref SIDPointerBufferSize, DomainNamePointer, ref DomainNameBufferSize, ref Use);
            // If the result is zero, it failed. Return the error code.
            if ((Result == 0))
            {
                return(GetLastError());
            }

            IntPtr SDDLString = new IntPtr();

            // We got the SID in the form of a byte array. So we have to convert this to a string. This function returns
            // non-zero on success.
            Result = SIDInfo.ConvertSidToStringSid(SIDPointer, ref SDDLString);
            if ((Result == 0))
            {
                return(GetLastError());
            }

            // Getting the account and domain values from the pointers we sent to LookupAccountName.
            Account = AccountName;
            SID     = Marshal.PtrToStringAnsi(SDDLString);
            Domain  = Marshal.PtrToStringAnsi(DomainNamePointer);
            SIDUse  = Use;
            // Free up the memory we used for the operation.
            Marshal.FreeHGlobal(SIDPointer);
            Marshal.FreeHGlobal(DomainNamePointer);
            SIDInfo.LocalFree(SDDLString.ToInt32());
            return(0);
        }
예제 #2
0
파일: SidInfo.cs 프로젝트: Yikez978/aTree
        // '' <summary>
        // '' Gets account info using an SID through the LookupAccountSid function.
        // '' </summary>
        // '' <param name="SID">The SID to use as a lookup parameter.</param>
        // '' <param name="System">The system to search for the SID, or blank if the SID is not for a local account.</param>
        // '' <param name="Account">A reference to a string that will contain the Account.</param>
        // '' <param name="Domain">A reference to a string that will contain the Domain.</param>
        // '' <param name="SIDUse">A reference to an integer that will contain the SID type.</param>
        // '' <returns></returns>
        // '' <remarks></remarks>
        public static int GetAccountInfoBySID(string SID, string System, ref string Account, ref string Domain, ref int SIDUse)
        {
            long   Result;
            int    SIDInteger;
            IntPtr SIDPointer = new IntPtr();

            // Convert the string SID to a byte array. If this returns false, then the function failed, return last error.
            if (!SIDInfo.ConvertStringSidToSid(SID, ref SIDPointer))
            {
                return(GetLastError());
            }

            SIDInteger = SIDPointer.ToInt32();
            // The buffer sizes can be adjusted if desired, but the return values will probably never exceed 255.
            // So this is a good, safe value.
            IntPtr AccountNamePointer    = new IntPtr();
            int    AccountNameBufferSize = 255;
            IntPtr DomainNamePointer     = new IntPtr();
            int    DomainNameBufferSize  = 255;
            int    SIDType = 0;

            // Since we're dealing with unmanaged code, we have to allocate memory for these manually.
            AccountNamePointer = Marshal.AllocHGlobal(AccountNameBufferSize);
            DomainNamePointer  = Marshal.AllocHGlobal(DomainNameBufferSize);
            // Calling the function, the return value will contain either 0 on failure, or non-zero on success.
            Result = SIDInfo.LookupAccountSid(System, SIDInteger, AccountNamePointer, ref AccountNameBufferSize, DomainNamePointer, ref DomainNameBufferSize, ref SIDType);
            // If the result is zero, it failed. Return the error code.
            if ((Result == 0))
            {
                return(GetLastError());
            }

            // Getting the account and domain values from the pointers we sent to LookupAccountSid.
            Account = Marshal.PtrToStringAnsi(AccountNamePointer);
            Domain  = Marshal.PtrToStringAnsi(DomainNamePointer);
            SIDUse  = SIDType;
            // Free up the memory we used for the operation.
            Marshal.FreeHGlobal(AccountNamePointer);
            Marshal.FreeHGlobal(DomainNamePointer);
            SIDInfo.LocalFree(SIDPointer.ToInt32());
            return(0);
        }
예제 #3
0
파일: SidInfo.cs 프로젝트: Yikez978/aTree
        // '' <summary>
        // '' Creates a new instance of the SIDInfo class.
        // '' </summary>
        // '' <param name="Identity">A string containing identifying information about the account, SID or NTAccount.</param>
        // '' <param name="SystemName">A string containing the name of the system in which to search for the SID.</param>
        // '' <param name="Type">Specifies the type of data in the Identity parameter. Default is SID.</param>
        // '' <remarks></remarks>
        public SIDInfo(string Identity, string SystemName, IdentityType IdentityType)
        {
            string Account = string.Empty;
            // Warning!!! Optional parameters not supported
            // Warning!!! Optional parameters not supported
            string Domain = string.Empty;
            int    SIDUse = 0;
            string SID    = string.Empty;
            string System = SystemName;

            if ((IdentityType == IdentityType.SID))
            {
                SID = Identity;
                int Result;
                Result = SIDInfo.GetAccountInfoBySID(Identity, SystemName, ref Account, ref Domain, ref SIDUse);
                if ((Result != 0))
                {
                    throw new ApplicationException(("Unable to get account info by SID, error code: " + Result.ToString()));
                }
            }
            else
            {
                int Result;
                Result = SIDInfo.GetAccountInfoByName(Identity, SystemName, ref SID, ref Domain, ref SIDUse);
                if ((Result != 0))
                {
                    throw new ApplicationException(("Unable to get account info by name, error code: " + Result.ToString()));
                }

                Result = SIDInfo.GetAccountInfoBySID(SID, SystemName, ref Account, ref Domain, ref SIDUse);
                if ((Result != 0))
                {
                    throw new ApplicationException(("Unable to get account info by SID, error code: " + Result.ToString()));
                }
            }

            _Account = Account;
            _Domain  = Domain;
            _SIDUse  = SIDUse;
            _SID     = SID;
            _System  = SystemName;
        }