public static WindowsIdentity GetAnonymousIdentity() { SafeCloseHandle tokenHandle = null; bool flag = false; lock (lockObject) { if (anonymousIdentity == null) { try { try { if (!SafeNativeMethods.ImpersonateAnonymousUserOnCurrentThread(SafeNativeMethods.GetCurrentThread())) { int error = Marshal.GetLastWin32Error(); throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error, System.ServiceModel.SR.GetString("ImpersonateAnonymousTokenFailed", new object[] { error }))); } flag = true; if (!SafeNativeMethods.OpenCurrentThreadToken(SafeNativeMethods.GetCurrentThread(), TokenAccessLevels.Query, true, out tokenHandle)) { int num2 = Marshal.GetLastWin32Error(); if (!SafeNativeMethods.RevertToSelf()) { num2 = Marshal.GetLastWin32Error(); System.ServiceModel.DiagnosticUtility.FailFast("RevertToSelf() failed with " + num2); } flag = false; Utility.CloseInvalidOutSafeHandle(tokenHandle); throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(num2, System.ServiceModel.SR.GetString("OpenThreadTokenFailed", new object[] { num2 }))); } if (!SafeNativeMethods.RevertToSelf()) { System.ServiceModel.DiagnosticUtility.FailFast("RevertToSelf() failed with " + Marshal.GetLastWin32Error()); } flag = false; using (tokenHandle) { anonymousIdentity = new WindowsIdentity(tokenHandle.DangerousGetHandle()); } } finally { if (flag && !SafeNativeMethods.RevertToSelf()) { System.ServiceModel.DiagnosticUtility.FailFast("RevertToSelf() failed with " + Marshal.GetLastWin32Error()); } } } catch { throw; } } } return(anonymousIdentity); }
public static WindowsIdentity GetAnonymousIdentity() { SafeCloseHandle tokenHandle = null; bool isImpersonating = false; lock (lockObject) { if (anonymousIdentity == null) { try { try { if (!SafeNativeMethods.ImpersonateAnonymousUserOnCurrentThread(SafeNativeMethods.GetCurrentThread())) { int error = Marshal.GetLastWin32Error(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error, SR.GetString(SR.ImpersonateAnonymousTokenFailed, error))); } isImpersonating = true; bool revertSuccess; bool isSuccess = SafeNativeMethods.OpenCurrentThreadToken(SafeNativeMethods.GetCurrentThread(), TokenAccessLevels.Query, true, out tokenHandle); if (!isSuccess) { int error = Marshal.GetLastWin32Error(); revertSuccess = SafeNativeMethods.RevertToSelf(); if (false == revertSuccess) { error = Marshal.GetLastWin32Error(); //this requires a failfast since failure to revert impersonation compromises security DiagnosticUtility.FailFast("RevertToSelf() failed with " + error); } isImpersonating = false; Utility.CloseInvalidOutSafeHandle(tokenHandle); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error, SR.GetString(SR.OpenThreadTokenFailed, error))); } revertSuccess = SafeNativeMethods.RevertToSelf(); if (false == revertSuccess) { int error = Marshal.GetLastWin32Error(); //this requires a failfast since failure to revert impersonation compromises security DiagnosticUtility.FailFast("RevertToSelf() failed with " + error); } isImpersonating = false; using (tokenHandle) { anonymousIdentity = new WindowsIdentity(tokenHandle.DangerousGetHandle()); } } finally { if (isImpersonating) { bool revertSuccess = SafeNativeMethods.RevertToSelf(); if (false == revertSuccess) { int error = Marshal.GetLastWin32Error(); //this requires a failfast since failure to revert impersonation compromises security DiagnosticUtility.FailFast("RevertToSelf() failed with " + error); } } } } catch { // Force the finally to run before leaving the method. throw; } } } return(anonymousIdentity); }