mscorlib(4.0.0.0) API with additions
WindowsImpersonationContext.cs
1 using Microsoft.Win32;
2 using Microsoft.Win32.SafeHandles;
6 
8 {
10  [ComVisible(true)]
12  {
13  [SecurityCritical]
14  private SafeAccessTokenHandle m_safeTokenHandle = SafeAccessTokenHandle.InvalidHandle;
15 
16  private WindowsIdentity m_wi;
17 
18  private FrameSecurityDescriptor m_fsd;
19 
20  [SecurityCritical]
22  {
23  }
24 
25  [SecurityCritical]
26  internal WindowsImpersonationContext(SafeAccessTokenHandle safeTokenHandle, WindowsIdentity wi, bool isImpersonating, FrameSecurityDescriptor fsd)
27  {
28  if (safeTokenHandle.IsInvalid)
29  {
30  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidImpersonationToken"));
31  }
32  if (isImpersonating)
33  {
34  if (!Win32Native.DuplicateHandle(Win32Native.GetCurrentProcess(), safeTokenHandle, Win32Native.GetCurrentProcess(), ref m_safeTokenHandle, 0u, bInheritHandle: true, 2u))
35  {
36  throw new SecurityException(Win32Native.GetMessage(Marshal.GetLastWin32Error()));
37  }
38  m_wi = wi;
39  }
40  m_fsd = fsd;
41  }
42 
45  [SecuritySafeCritical]
46  public void Undo()
47  {
48  int num = 0;
49  if (m_safeTokenHandle.IsInvalid)
50  {
51  num = Win32.RevertToSelf();
52  if (num < 0)
53  {
54  Environment.FailFast(Win32Native.GetMessage(num));
55  }
56  }
57  else
58  {
59  num = Win32.RevertToSelf();
60  if (num < 0)
61  {
62  Environment.FailFast(Win32Native.GetMessage(num));
63  }
64  num = Win32.ImpersonateLoggedOnUser(m_safeTokenHandle);
65  if (num < 0)
66  {
67  throw new SecurityException(Win32Native.GetMessage(num));
68  }
69  }
70  WindowsIdentity.UpdateThreadWI(m_wi);
71  if (m_fsd != null)
72  {
73  m_fsd.SetTokenHandles(null, null);
74  }
75  }
76 
77  [SecurityCritical]
78  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
79  [HandleProcessCorruptedStateExceptions]
80  internal bool UndoNoThrow()
81  {
82  bool flag = false;
83  try
84  {
85  int num = 0;
86  if (m_safeTokenHandle.IsInvalid)
87  {
88  num = Win32.RevertToSelf();
89  if (num < 0)
90  {
91  Environment.FailFast(Win32Native.GetMessage(num));
92  }
93  }
94  else
95  {
96  num = Win32.RevertToSelf();
97  if (num >= 0)
98  {
99  num = Win32.ImpersonateLoggedOnUser(m_safeTokenHandle);
100  }
101  else
102  {
103  Environment.FailFast(Win32Native.GetMessage(num));
104  }
105  }
106  flag = (num >= 0);
107  if (m_fsd == null)
108  {
109  return flag;
110  }
111  m_fsd.SetTokenHandles(null, null);
112  return flag;
113  }
114  catch
115  {
116  return false;
117  }
118  }
119 
123  [SecuritySafeCritical]
124  [ComVisible(false)]
125  protected virtual void Dispose(bool disposing)
126  {
127  if (disposing && m_safeTokenHandle != null && !m_safeTokenHandle.IsClosed)
128  {
129  Undo();
130  m_safeTokenHandle.Dispose();
131  }
132  }
133 
135  [ComVisible(false)]
136  public void Dispose()
137  {
138  Dispose(disposing: true);
139  }
140  }
141 }
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
Definition: __Canon.cs:3
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.Security.Principal.WindowsImpersonationContext ...
void Undo()
Reverts the user context to the Windows user represented by this object.
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
void Dispose()
Releases all resources used by the T:System.Security.Principal.WindowsImpersonationContext.
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
The exception that is thrown when one of the arguments provided to a method is not valid.
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
The exception that is thrown when a security error is detected.
Represents the Windows user prior to an impersonation operation.
static void FailFast(string message)
Immediately terminates a process after writing a message to the Windows Application event log,...