Code:
/ DotNET / DotNET / 8.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
- SolidBrush.cs
- DirectoryObjectSecurity.cs
- SqlSelectStatement.cs
- Exceptions.cs
- AppDomainManager.cs
- ImageCodecInfo.cs
- ReadOnlyDictionary.cs
- ConstNode.cs
- VisualStyleElement.cs
- StreamUpdate.cs
- AutomationElement.cs
- KeyBinding.cs
- DataBoundControlAdapter.cs
- DocumentPageView.cs
- FactoryGenerator.cs
- ComboBox.cs
- SafeTokenHandle.cs
- TableLayoutRowStyleCollection.cs
- FixedPageProcessor.cs
- parserscommon.cs
- ClickablePoint.cs
- BuildProvider.cs
- StickyNoteAnnotations.cs
- TextCollapsingProperties.cs
- GenerateScriptTypeAttribute.cs
- SystemWebSectionGroup.cs
- CheckBoxFlatAdapter.cs
- File.cs
- TextOutput.cs
- CompilerInfo.cs
- ValidationRuleCollection.cs
- DebugView.cs
- BrushMappingModeValidation.cs
- DateTimeConverter.cs
- EntitySqlQueryBuilder.cs
- Action.cs
- basecomparevalidator.cs
- DesignerUtility.cs
- BaseUriHelper.cs
- OdbcConnectionOpen.cs
- EntityTypeEmitter.cs
- XappLauncher.cs
- EntitySqlQueryCacheEntry.cs
- RuntimeEnvironment.cs
- DateTimeOffsetAdapter.cs
- ArraySet.cs
- OdbcException.cs
- GeneralTransform3D.cs
- QueryPageSettingsEventArgs.cs
- EventProxy.cs
- TextMetrics.cs
- ViewBase.cs
- SqlMethodAttribute.cs
- InvokeProviderWrapper.cs
- BindingBase.cs
- DataViewManager.cs
- UnlockInstanceAsyncResult.cs
- Splitter.cs
- SqlException.cs
- Mappings.cs
- PartialCachingControl.cs
- Parser.cs
- HealthMonitoringSectionHelper.cs
- precedingsibling.cs
- TrackingMemoryStream.cs
- SiteMembershipCondition.cs
- Menu.cs
- DecimalSumAggregationOperator.cs
- WebControl.cs
- IfAction.cs
- DynamicControl.cs
- TextStore.cs
- HostExecutionContextManager.cs
- HttpValueCollection.cs
- SQLInt32.cs
- SHA512.cs
- TripleDES.cs
- MetadataArtifactLoaderXmlReaderWrapper.cs
- DiscreteKeyFrames.cs
- UnmanagedMarshal.cs
- CodeActivityContext.cs
- ClickablePoint.cs
- GenericAuthenticationEventArgs.cs
- TemplatedEditableDesignerRegion.cs
- DisplayInformation.cs
- HtmlInputSubmit.cs
- SettingsPropertyNotFoundException.cs
- safex509handles.cs
- KeyValueConfigurationElement.cs
- WsdlInspector.cs
- FontFamilyIdentifier.cs
- SafeRightsManagementQueryHandle.cs
- ContentWrapperAttribute.cs
- SymmetricKey.cs
- BinaryExpression.cs
- ZoneLinkButton.cs
- Decorator.cs
- ParameterDataSourceExpression.cs
- EntityCommandCompilationException.cs
- SynchronizedInputHelper.cs