Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / 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
- ProfilePropertyMetadata.cs
- SafeWaitHandle.cs
- ConfigurationErrorsException.cs
- securitymgrsite.cs
- XsltQilFactory.cs
- NamespaceDisplayAutomationPeer.cs
- XNameConverter.cs
- SQLChars.cs
- _ListenerResponseStream.cs
- Content.cs
- SimpleType.cs
- RawUIStateInputReport.cs
- VectorAnimationBase.cs
- Int64Converter.cs
- User.cs
- X509CertificateRecipientClientCredential.cs
- LoginCancelEventArgs.cs
- ObjectStateEntryDbUpdatableDataRecord.cs
- WebFaultException.cs
- StyleHelper.cs
- DrawingAttributes.cs
- XmlDataDocument.cs
- ParameterBinding.cs
- Literal.cs
- ChannelFactoryRefCache.cs
- FileChangesMonitor.cs
- ProcessModelInfo.cs
- Version.cs
- NeutralResourcesLanguageAttribute.cs
- DbConnectionPoolOptions.cs
- WebPartDescriptionCollection.cs
- StateRuntime.cs
- BypassElement.cs
- _DigestClient.cs
- EntityDataSourceReferenceGroup.cs
- Base64Decoder.cs
- InputLanguageManager.cs
- XmlTextReaderImplHelpers.cs
- SelectionItemPattern.cs
- RequestSecurityToken.cs
- Baml2006KnownTypes.cs
- OleDbPropertySetGuid.cs
- WindowsSpinner.cs
- VersionedStreamOwner.cs
- FacetValueContainer.cs
- ProjectionCamera.cs
- UniqueConstraint.cs
- RequestQueue.cs
- CharEnumerator.cs
- ISFTagAndGuidCache.cs
- RenamedEventArgs.cs
- PathData.cs
- ClientRuntimeConfig.cs
- WebPartEditorCancelVerb.cs
- SerializationSectionGroup.cs
- BitmapInitialize.cs
- SelectionEditor.cs
- CapabilitiesUse.cs
- Win32KeyboardDevice.cs
- WebPartEditorCancelVerb.cs
- VisualBasicImportReference.cs
- GridViewCancelEditEventArgs.cs
- FontStretch.cs
- DbConnectionPoolIdentity.cs
- Enlistment.cs
- PlainXmlWriter.cs
- CreateBookmarkScope.cs
- DoWhile.cs
- ToolStripActionList.cs
- UrlAuthorizationModule.cs
- BitConverter.cs
- NetNamedPipeSecurityElement.cs
- ErrorInfoXmlDocument.cs
- XamlVector3DCollectionSerializer.cs
- CharacterBufferReference.cs
- MarginsConverter.cs
- CodeRemoveEventStatement.cs
- MetadataFile.cs
- GridViewRowEventArgs.cs
- ParallelTimeline.cs
- DBSchemaRow.cs
- WebPageTraceListener.cs
- ObjectDataSourceSelectingEventArgs.cs
- BitmapEffectvisualstate.cs
- TrackBarDesigner.cs
- SingleTagSectionHandler.cs
- DependencyPropertyKind.cs
- SmtpAuthenticationManager.cs
- WorkflowRuntimeServiceElement.cs
- ListComponentEditor.cs
- UxThemeWrapper.cs
- ByteStream.cs
- NonClientArea.cs
- WindowsGrip.cs
- MDIControlStrip.cs
- PageContent.cs
- Utils.cs
- CatalogZone.cs
- SamlEvidence.cs
- ScrollBarRenderer.cs