Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / 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); } } }
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ArrayTypeMismatchException.cs
- TypeSemantics.cs
- DocumentPaginator.cs
- ColorConvertedBitmapExtension.cs
- DesignerActionList.cs
- InputScopeConverter.cs
- Mapping.cs
- Properties.cs
- CipherData.cs
- ReceiveParametersContent.cs
- AbandonedMutexException.cs
- DataGridViewColumn.cs
- DataPagerFieldItem.cs
- MobileUITypeEditor.cs
- XmlNamespaceManager.cs
- RegexCompilationInfo.cs
- WebPartManager.cs
- SqlDataSourceCache.cs
- LocalizableAttribute.cs
- Rect3DValueSerializer.cs
- XmlLinkedNode.cs
- ChameleonKey.cs
- XmlAtomicValue.cs
- CreatingCookieEventArgs.cs
- GroupStyle.cs
- IndexOutOfRangeException.cs
- StylusButtonEventArgs.cs
- AnnotationHighlightLayer.cs
- ChangeInterceptorAttribute.cs
- PageStatePersister.cs
- TraceUtility.cs
- IssuanceLicense.cs
- CodeCommentStatementCollection.cs
- PropertyConverter.cs
- COM2AboutBoxPropertyDescriptor.cs
- SecurityToken.cs
- Page.cs
- CacheDependency.cs
- SqlConnectionPoolProviderInfo.cs
- Documentation.cs
- X509ScopedServiceCertificateElementCollection.cs
- GACMembershipCondition.cs
- Vector3DConverter.cs
- DataView.cs
- HashCodeCombiner.cs
- newinstructionaction.cs
- StackSpiller.Generated.cs
- ObjectConverter.cs
- EventMappingSettings.cs
- SystemShuttingDownException.cs
- QueryOperatorEnumerator.cs
- SiteMapNodeCollection.cs
- GridView.cs
- TypeHelpers.cs
- ColorKeyFrameCollection.cs
- PropertyCollection.cs
- CallId.cs
- DeclarationUpdate.cs
- DoubleLink.cs
- AssemblyBuilder.cs
- Ray3DHitTestResult.cs
- HtmlTextArea.cs
- MimeTypeMapper.cs
- DataGridBoolColumn.cs
- SiteMapSection.cs
- ActivityDefaults.cs
- BamlLocalizer.cs
- LexicalChunk.cs
- WpfWebRequestHelper.cs
- TextUtf8RawTextWriter.cs
- DocumentEventArgs.cs
- DependencyPropertyHelper.cs
- PinnedBufferMemoryStream.cs
- ProxyWebPartConnectionCollection.cs
- ConvertersCollection.cs
- Solver.cs
- CanonicalizationDriver.cs
- XmlStringTable.cs
- DataStreams.cs
- DecimalKeyFrameCollection.cs
- ToolStripProfessionalLowResolutionRenderer.cs
- FullTextState.cs
- WebPartConnectionsConfigureVerb.cs
- UpdateExpressionVisitor.cs
- AVElementHelper.cs
- QilTargetType.cs
- TrustManagerMoreInformation.cs
- DataGridViewBand.cs
- ZipIOExtraFieldZip64Element.cs
- NativeMethods.cs
- ClientConfigurationSystem.cs
- WindowsToolbar.cs
- PreservationFileReader.cs
- KeyValuePairs.cs
- PagePropertiesChangingEventArgs.cs
- DataListItemCollection.cs
- SelectingProviderEventArgs.cs
- _CommandStream.cs
- Nodes.cs
- DataRecordInternal.cs