Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / clr / src / BCL / System / Security / Principal / WindowsImpersonationContext.cs / 1 / WindowsImpersonationContext.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // // WindowsImpersonationContext.cs // // Representation of an impersonation context. // namespace System.Security.Principal { using Microsoft.Win32; using Microsoft.Win32.SafeHandles; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Runtime.ConstrainedExecution; [System.Runtime.InteropServices.ComVisible(true)] public class WindowsImpersonationContext : IDisposable { private SafeTokenHandle m_safeTokenHandle = SafeTokenHandle.InvalidHandle; private WindowsIdentity m_wi; private FrameSecurityDescriptor m_fsd; private WindowsImpersonationContext () {} internal WindowsImpersonationContext (SafeTokenHandle safeTokenHandle, WindowsIdentity wi, bool isImpersonating, FrameSecurityDescriptor fsd) { // make this a no-op on Win98 so calling code does not have to special case down-level platforms. if (WindowsIdentity.RunningOnWin2K) { if (safeTokenHandle.IsInvalid) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidImpersonationToken")); if (isImpersonating) { if (!Win32Native.DuplicateHandle(Win32Native.GetCurrentProcess(), safeTokenHandle, Win32Native.GetCurrentProcess(), ref m_safeTokenHandle, 0, true, Win32Native.DUPLICATE_SAME_ACCESS)) throw new SecurityException(Win32Native.GetMessage(Marshal.GetLastWin32Error())); m_wi = wi; } m_fsd = fsd; } } // Revert to previous impersonation (the only public method). public void Undo () { // make this a no-op on Win98 so calling code does not have to special case down-level platforms. if (!WindowsIdentity.RunningOnWin2K) return; int hr = 0; if (m_safeTokenHandle.IsInvalid) { // the thread was not initially impersonating hr = Win32.RevertToSelf(); if (hr < 0) throw new SecurityException(Win32Native.GetMessage(hr)); } else { hr = Win32.RevertToSelf(); if (hr < 0) throw new SecurityException(Win32Native.GetMessage(hr)); hr = Win32.ImpersonateLoggedOnUser(m_safeTokenHandle); if (hr < 0) throw new SecurityException(Win32Native.GetMessage(hr)); } WindowsIdentity.UpdateThreadWI(m_wi); if (m_fsd != null) m_fsd.SetTokenHandles(null, null); } // Non-throwing version that does not new any exception objects. To be called when reliability matters [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal bool UndoNoThrow() { bool bRet = false; try{ // make this a no-op on Win98 so calling code does not have to special case down-level platforms. if (!WindowsIdentity.RunningOnWin2K) return true; int hr = 0; if (m_safeTokenHandle.IsInvalid) { // the thread was not initially impersonating hr = Win32.RevertToSelf(); } else { hr = Win32.RevertToSelf(); if (hr >= 0) hr = Win32.ImpersonateLoggedOnUser(m_safeTokenHandle); } bRet = (hr >= 0); if (m_fsd != null) m_fsd.SetTokenHandles(null,null); } catch { bRet = false; } return bRet; } // // IDisposable interface. // [ComVisible(false)] protected virtual void Dispose(bool disposing) { if (disposing) { if (m_safeTokenHandle != null && !m_safeTokenHandle.IsClosed) { Undo(); m_safeTokenHandle.Dispose(); } } } [ComVisible(false)] public void Dispose () { Dispose(true); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // // WindowsImpersonationContext.cs // // Representation of an impersonation context. // namespace System.Security.Principal { using Microsoft.Win32; using Microsoft.Win32.SafeHandles; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Runtime.ConstrainedExecution; [System.Runtime.InteropServices.ComVisible(true)] public class WindowsImpersonationContext : IDisposable { private SafeTokenHandle m_safeTokenHandle = SafeTokenHandle.InvalidHandle; private WindowsIdentity m_wi; private FrameSecurityDescriptor m_fsd; private WindowsImpersonationContext () {} internal WindowsImpersonationContext (SafeTokenHandle safeTokenHandle, WindowsIdentity wi, bool isImpersonating, FrameSecurityDescriptor fsd) { // make this a no-op on Win98 so calling code does not have to special case down-level platforms. if (WindowsIdentity.RunningOnWin2K) { if (safeTokenHandle.IsInvalid) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidImpersonationToken")); if (isImpersonating) { if (!Win32Native.DuplicateHandle(Win32Native.GetCurrentProcess(), safeTokenHandle, Win32Native.GetCurrentProcess(), ref m_safeTokenHandle, 0, true, Win32Native.DUPLICATE_SAME_ACCESS)) throw new SecurityException(Win32Native.GetMessage(Marshal.GetLastWin32Error())); m_wi = wi; } m_fsd = fsd; } } // Revert to previous impersonation (the only public method). public void Undo () { // make this a no-op on Win98 so calling code does not have to special case down-level platforms. if (!WindowsIdentity.RunningOnWin2K) return; int hr = 0; if (m_safeTokenHandle.IsInvalid) { // the thread was not initially impersonating hr = Win32.RevertToSelf(); if (hr < 0) throw new SecurityException(Win32Native.GetMessage(hr)); } else { hr = Win32.RevertToSelf(); if (hr < 0) throw new SecurityException(Win32Native.GetMessage(hr)); hr = Win32.ImpersonateLoggedOnUser(m_safeTokenHandle); if (hr < 0) throw new SecurityException(Win32Native.GetMessage(hr)); } WindowsIdentity.UpdateThreadWI(m_wi); if (m_fsd != null) m_fsd.SetTokenHandles(null, null); } // Non-throwing version that does not new any exception objects. To be called when reliability matters [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal bool UndoNoThrow() { bool bRet = false; try{ // make this a no-op on Win98 so calling code does not have to special case down-level platforms. if (!WindowsIdentity.RunningOnWin2K) return true; int hr = 0; if (m_safeTokenHandle.IsInvalid) { // the thread was not initially impersonating hr = Win32.RevertToSelf(); } else { hr = Win32.RevertToSelf(); if (hr >= 0) hr = Win32.ImpersonateLoggedOnUser(m_safeTokenHandle); } bRet = (hr >= 0); if (m_fsd != null) m_fsd.SetTokenHandles(null,null); } catch { bRet = false; } return bRet; } // // IDisposable interface. // [ComVisible(false)] protected virtual void Dispose(bool disposing) { if (disposing) { if (m_safeTokenHandle != null && !m_safeTokenHandle.IsClosed) { Undo(); m_safeTokenHandle.Dispose(); } } } [ComVisible(false)] public void Dispose () { Dispose(true); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- HtmlMeta.cs
- RTLAwareMessageBox.cs
- SmtpException.cs
- XmlTextReaderImplHelpers.cs
- QilValidationVisitor.cs
- TypeConvertions.cs
- CompletedAsyncResult.cs
- FloaterBaseParaClient.cs
- XmlSchemaInferenceException.cs
- CancellationTokenSource.cs
- ControlEvent.cs
- AutoGeneratedField.cs
- WmfPlaceableFileHeader.cs
- ParsedAttributeCollection.cs
- WebControlAdapter.cs
- SoapReflectionImporter.cs
- NativeCppClassAttribute.cs
- CommentEmitter.cs
- mda.cs
- BitmapFrame.cs
- _NegoState.cs
- ReversePositionQuery.cs
- HttpRequestCacheValidator.cs
- DataGridViewRowDividerDoubleClickEventArgs.cs
- CodePropertyReferenceExpression.cs
- KeyPressEvent.cs
- RenderTargetBitmap.cs
- SiteMapDataSourceView.cs
- CollectionView.cs
- DataGridViewHeaderCell.cs
- Expressions.cs
- LayoutDump.cs
- Figure.cs
- InputLanguage.cs
- VectorAnimationBase.cs
- FileResponseElement.cs
- Choices.cs
- AstTree.cs
- TagPrefixCollection.cs
- ModelUtilities.cs
- Menu.cs
- MediaSystem.cs
- XmlSubtreeReader.cs
- WSUtilitySpecificationVersion.cs
- CompilerResults.cs
- Socket.cs
- FontCollection.cs
- TimeSpanMinutesOrInfiniteConverter.cs
- ClearTypeHintValidation.cs
- PersistChildrenAttribute.cs
- Context.cs
- HMACMD5.cs
- xmlglyphRunInfo.cs
- DrawListViewColumnHeaderEventArgs.cs
- SqlDependency.cs
- XmlQualifiedNameTest.cs
- TypeExtension.cs
- TraceSection.cs
- BamlResourceContent.cs
- SharedMemory.cs
- HeaderCollection.cs
- InfoCardCryptoHelper.cs
- smtppermission.cs
- Compiler.cs
- CommunicationException.cs
- FileVersion.cs
- TreeNodeSelectionProcessor.cs
- ObjectStateManager.cs
- DispatcherObject.cs
- FixedSOMImage.cs
- TransformGroup.cs
- AccessDataSource.cs
- MethodBuilder.cs
- JsonMessageEncoderFactory.cs
- WmlFormAdapter.cs
- UInt32.cs
- HttpApplicationFactory.cs
- WindowsListViewScroll.cs
- CharacterBufferReference.cs
- NavigatorInput.cs
- RoutedEventHandlerInfo.cs
- TypedElement.cs
- COM2PropertyDescriptor.cs
- InstanceStoreQueryResult.cs
- Solver.cs
- ConfigsHelper.cs
- DictationGrammar.cs
- TextRenderingModeValidation.cs
- Int64.cs
- MenuAdapter.cs
- ExpressionValueEditor.cs
- SchemaTypeEmitter.cs
- ExceptionRoutedEventArgs.cs
- StrongNameMembershipCondition.cs
- HttpHandlersSection.cs
- SuppressMergeCheckAttribute.cs
- TypeSystem.cs
- Grid.cs
- CreateSequence.cs
- Thumb.cs