Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / untmp / whidbey / QFE / ndp / clr / src / BCL / System / Security / Principal / WindowsImpersonationContext.cs / 2 / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Verify.cs
- WebPart.cs
- ListDictionaryInternal.cs
- SqlDataSourceStatusEventArgs.cs
- HtmlInputControl.cs
- SetStateDesigner.cs
- TextBox.cs
- ScriptReferenceBase.cs
- PropertyKey.cs
- OpCopier.cs
- DesignSurfaceServiceContainer.cs
- GenerateScriptTypeAttribute.cs
- NativeMethods.cs
- TypeUtils.cs
- PersonalizablePropertyEntry.cs
- SystemResourceKey.cs
- DbProviderManifest.cs
- RectAnimationUsingKeyFrames.cs
- ExpandableObjectConverter.cs
- CodePrimitiveExpression.cs
- FixUp.cs
- RowUpdatedEventArgs.cs
- TextEditorThreadLocalStore.cs
- RecognizerBase.cs
- PostBackOptions.cs
- OdbcEnvironmentHandle.cs
- CharacterBuffer.cs
- DataTableTypeConverter.cs
- ReadOnlyActivityGlyph.cs
- OracleEncoding.cs
- EmptyReadOnlyDictionaryInternal.cs
- InputProviderSite.cs
- CodeDirectoryCompiler.cs
- CheckBoxStandardAdapter.cs
- DoubleCollection.cs
- BadImageFormatException.cs
- StyleModeStack.cs
- State.cs
- MsmqHostedTransportConfiguration.cs
- ImageMap.cs
- ActivationArguments.cs
- ApplicationBuildProvider.cs
- XmlResolver.cs
- AutoResizedEvent.cs
- SerializationAttributes.cs
- DataGridViewColumnConverter.cs
- DBSqlParserColumnCollection.cs
- SafeSecurityHandles.cs
- DataGridViewCheckBoxCell.cs
- NonDualMessageSecurityOverHttpElement.cs
- ImportException.cs
- EmptyQuery.cs
- ReachBasicContext.cs
- RoleManagerModule.cs
- UIElement3DAutomationPeer.cs
- ConstrainedDataObject.cs
- uribuilder.cs
- FusionWrap.cs
- InfoCardBaseException.cs
- FrameworkElementFactory.cs
- MediaElement.cs
- CodePropertyReferenceExpression.cs
- GeometryDrawing.cs
- GeneralTransform3D.cs
- DispatcherOperation.cs
- MetadataItemSerializer.cs
- IdentityNotMappedException.cs
- CellConstantDomain.cs
- LogExtent.cs
- SqlFactory.cs
- KeyToListMap.cs
- BindingNavigator.cs
- ZipIOBlockManager.cs
- StylusDownEventArgs.cs
- ResourceReferenceKeyNotFoundException.cs
- QueueTransferProtocol.cs
- TextEncodedRawTextWriter.cs
- ResourcePart.cs
- CultureSpecificStringDictionary.cs
- Int32AnimationUsingKeyFrames.cs
- TemplateField.cs
- TransformerInfoCollection.cs
- TextFormatterContext.cs
- XmlByteStreamWriter.cs
- DataAdapter.cs
- TextSegment.cs
- TransportListener.cs
- ServiceModelActivationSectionGroup.cs
- sortedlist.cs
- MD5CryptoServiceProvider.cs
- DataSetMappper.cs
- BulletedListEventArgs.cs
- TargetConverter.cs
- ServicesUtilities.cs
- CustomErrorsSectionWrapper.cs
- DescendantQuery.cs
- Registry.cs
- SQLGuidStorage.cs
- ObservableDictionary.cs
- X509Chain.cs