private void SetTokenMandatoryLabel(IntPtr token, SecurityMandatoryLabel securityMandatoryLabel)
        {
            // Create the low integrity SID.
            IntPtr integritySid;

            if (!NativeMethods.AllocateAndInitializeSid(
                    ref NativeMethods.SECURITY_MANDATORY_LABEL_AUTHORITY,
                    1,
                    (int)securityMandatoryLabel,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    out integritySid))
            {
                throw new Win32Exception();
            }

            var tokenMandatoryLabel = new TokenMandatoryLabel {
                Label = default(SidAndAttributes)
            };

            tokenMandatoryLabel.Label.Attributes = NativeMethods.SE_GROUP_INTEGRITY;
            tokenMandatoryLabel.Label.Sid        = integritySid;
            //// Marshal the TOKEN_MANDATORY_LABEL structure to the native memory.
            var sizeOfTokenMandatoryLabel = Marshal.SizeOf(tokenMandatoryLabel);
            var tokenInfo = Marshal.AllocHGlobal(sizeOfTokenMandatoryLabel);

            Marshal.StructureToPtr(tokenMandatoryLabel, tokenInfo, false);

            // Set the integrity level in the access token
            if (!NativeMethods.SetTokenInformation(
                    token,
                    TokenInformationClass.TokenIntegrityLevel,
                    tokenInfo,
                    sizeOfTokenMandatoryLabel + NativeMethods.GetLengthSid(integritySid)))
            {
                throw new Win32Exception();
            }

            //// SafeNativeMethods.CloseHandle(integritySid);
            //// SafeNativeMethods.CloseHandle(tokenInfo);
        }
        private void SetTokenMandatoryLabel(IntPtr token, SecurityMandatoryLabel securityMandatoryLabel)
        {
            // Create the low integrity SID.
            IntPtr integritySid;
            if (!NativeMethods.AllocateAndInitializeSid(
                    ref NativeMethods.SECURITY_MANDATORY_LABEL_AUTHORITY,
                    1,
                    (int)securityMandatoryLabel,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    out integritySid))
            {
                throw new Win32Exception();
            }

            var tokenMandatoryLabel = new TokenMandatoryLabel { Label = new SidAndAttributes() };
            tokenMandatoryLabel.Label.Attributes = NativeMethods.SE_GROUP_INTEGRITY;
            tokenMandatoryLabel.Label.Sid = integritySid;
            //// Marshal the TOKEN_MANDATORY_LABEL structure to the native memory.
            var sizeOfTokenMandatoryLabel = Marshal.SizeOf(tokenMandatoryLabel);
            var tokenInfo = Marshal.AllocHGlobal(sizeOfTokenMandatoryLabel);
            Marshal.StructureToPtr(tokenMandatoryLabel, tokenInfo, false);

            // Set the integrity level in the access token
            if (!NativeMethods.SetTokenInformation(
                    token,
                    TokenInformationClass.TokenIntegrityLevel,
                    tokenInfo,
                    sizeOfTokenMandatoryLabel + NativeMethods.GetLengthSid(integritySid)))
            {
                throw new Win32Exception();
            }

            //// SafeNativeMethods.CloseHandle(integritySid);
            //// SafeNativeMethods.CloseHandle(tokenInfo);
        }