void Undo()
                {
                    // PreSharp Bug: Call 'Marshal.GetLastWin32Error' or 'Marshal.GetHRForLastWin32Error' before any other interop call.
#pragma warning suppress 56523 // The LastWin32Error can be ignored here.
                    Fx.Assert(this.threadHandle == SafeNativeMethods.GetCurrentThread(), "");
                    // We are in the Dispose method. If a failure occurs we just have to ignore it.
                    // PreSharp Bug: Call 'Marshal.GetLastWin32Error' or 'Marshal.GetHRForLastWin32Error' before any other interop call.
                    // #pragma warning suppress 56523 // The LastWin32Error can be ignored here.
                    if (!SafeNativeMethods.SetCurrentThreadToken(IntPtr.Zero, this.tokenHandle))
                    {
                        int error = Marshal.GetLastWin32Error();
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityException(SR.GetString(SR.RevertImpersonationFailure,
                                                                                                                     new Win32Exception(error).Message)));
                    }
                    tokenHandle.Close();
                }
            // This api simply check if the calling thread is process primary thread.
            // We are not trying to be smart if the impersonation to the same user as
            // process token since privileges could be different.
            bool IsImpersonatedContext()
            {
                SafeCloseHandle tokenHandle = null;

                if (!SafeNativeMethods.OpenCurrentThreadToken(
                        SafeNativeMethods.GetCurrentThread(),
                        TokenAccessLevels.Query,
                        true,
                        out tokenHandle))
                {
                    int error = Marshal.GetLastWin32Error();
                    Utility.CloseInvalidOutSafeHandle(tokenHandle);
                    if (error == (int)Win32Error.ERROR_NO_TOKEN)
                    {
                        return(false);
                    }
                    System.ServiceModel.Dispatcher.ErrorBehavior.ThrowAndCatch(new Win32Exception(error));
                    return(true);
                }
                tokenHandle.Close();
                return(true);
            }