public void Dispose(bool disposing)
        {
            RevertToSelf();
            IntPtr secCtx = SafeNativeMethods.CoSwitchCallContext(oldSecurityObject);

            if (IntPtr.Zero == secCtx)
            {
                // this has to be a failfast since not having a security context can compromise security
                DiagnosticUtility.FailFast("Security Context was should not be null");
            }

            if (Marshal.GetObjectForIUnknown(secCtx) != this)
            {
                // this has to be a failfast since being in the wrong security context can compromise security
                DiagnosticUtility.FailFast("Security Context was modified from underneath us");
            }
            Marshal.Release(secCtx);
            if (disposing)
            {
                clientIdentity = null;
                if (impersonateContext != null)
                {
                    impersonateContext.Dispose();
                }
            }
        }
        public ComPlusServerSecurity(WindowsIdentity clientIdentity, bool shouldUseCallContext)
        {
            if (null == clientIdentity)
            {
                throw Fx.AssertAndThrow("NULL Identity");
            }
            if (IntPtr.Zero == clientIdentity.Token)
            {
                throw Fx.AssertAndThrow("Token handle cannot be zero");
            }

            this.shouldUseCallContext = shouldUseCallContext;
            this.clientIdentity       = clientIdentity;
            IntPtr secCtx = Marshal.GetIUnknownForObject(this);

            try
            {
                oldSecurityObject = SafeNativeMethods.CoSwitchCallContext(secCtx);
            }
            catch
            {
                Marshal.Release(secCtx);

                throw;
            }
        }
        public void Dispose(bool disposing)
        {
            this.RevertToSelf();
            IntPtr pUnk = SafeNativeMethods.CoSwitchCallContext(this.oldSecurityObject);

            if (IntPtr.Zero == pUnk)
            {
                DiagnosticUtility.FailFast("Security Context was should not be null");
            }
            if (Marshal.GetObjectForIUnknown(pUnk) != this)
            {
                DiagnosticUtility.FailFast("Security Context was modified from underneath us");
            }
            Marshal.Release(pUnk);
            if (disposing)
            {
                this.clientIdentity = null;
                if (this.impersonateContext != null)
                {
                    this.impersonateContext.Dispose();
                }
            }
        }