System.Security.Principal.Win32.CreateSidFromString C# (CSharp) Method

CreateSidFromString() static private method

static private CreateSidFromString ( string stringSid, byte &resultSid ) : int
stringSid string
resultSid byte
return int
        internal static int CreateSidFromString(
            string stringSid,
            out byte[] resultSid
            )
        {
            int ErrorCode;
            IntPtr ByteArray = IntPtr.Zero;

            try
            {
                if (FALSE == Interop.Advapi32.ConvertStringSidToSid(stringSid, out ByteArray))
                {
                    ErrorCode = Marshal.GetLastWin32Error();
                    goto Error;
                }

                resultSid = ConvertIntPtrSidToByteArraySid(ByteArray);
            }
            finally
            {
                //
                // Now is a good time to get rid of the returned pointer
                //

                Interop.Kernel32.LocalFree(ByteArray);
            }

            //
            // Now invoke the SecurityIdentifier factory method to create the result
            //

            return Interop.Errors.ERROR_SUCCESS;

            Error:

            resultSid = null;
            return ErrorCode;
        }

Usage Example

Beispiel #1
0
        //
        // Constructs a SecurityIdentifier object from its string representation
        // Returns 'null' if string passed in is not a valid SID
        // NOTE: although there is a P/Invoke call involved in the implementation of this method,
        //       there is no security risk involved, so no security demand is being made.
        //


        public SecurityIdentifier(string sddlForm)
        {
            ArgumentNullException.ThrowIfNull(sddlForm);

            //
            // Call into the underlying O/S conversion routine
            //

            int error = Win32.CreateSidFromString(sddlForm, out byte[]? resultSid);

            if (error == Interop.Errors.ERROR_INVALID_SID)
            {
                throw new ArgumentException(SR.Argument_InvalidValue, nameof(sddlForm));
            }
            else if (error == Interop.Errors.ERROR_NOT_ENOUGH_MEMORY)
            {
                throw new OutOfMemoryException();
            }
            else if (error != Interop.Errors.ERROR_SUCCESS)
            {
                Debug.Fail($"Win32.CreateSidFromString returned unrecognized error {error}");
                throw new Win32Exception(error);
            }

            CreateFromBinaryForm(resultSid !, 0);
        }
All Usage Examples Of System.Security.Principal.Win32::CreateSidFromString