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
- ComponentEditorForm.cs
- ContainsRowNumberChecker.cs
- ConfigurationValidatorBase.cs
- Debug.cs
- TableStyle.cs
- ValueTypeFixupInfo.cs
- XmlAttributeCollection.cs
- LostFocusEventManager.cs
- EdmToObjectNamespaceMap.cs
- ResourceDictionaryCollection.cs
- MouseButton.cs
- ChangeConflicts.cs
- VariableExpressionConverter.cs
- FlatButtonAppearance.cs
- XNodeValidator.cs
- ReferenceSchema.cs
- DashStyle.cs
- FormatException.cs
- BorderGapMaskConverter.cs
- AppDomainProtocolHandler.cs
- FontDifferentiator.cs
- EventProxy.cs
- Empty.cs
- CompilerInfo.cs
- DesignerGenericWebPart.cs
- FunctionQuery.cs
- PackagePartCollection.cs
- MenuItemBindingCollection.cs
- LayoutUtils.cs
- Module.cs
- smtpconnection.cs
- Mapping.cs
- SingleConverter.cs
- PersistenceTypeAttribute.cs
- XmlSchema.cs
- DataGridViewTextBoxEditingControl.cs
- DataTemplateSelector.cs
- TextEvent.cs
- DataFormats.cs
- EndOfStreamException.cs
- TransactionScope.cs
- DbgUtil.cs
- FunctionImportMapping.ReturnTypeRenameMapping.cs
- RequestResizeEvent.cs
- WebSysDescriptionAttribute.cs
- MostlySingletonList.cs
- LayoutEvent.cs
- JournalEntry.cs
- TriggerCollection.cs
- BaseDataList.cs
- BitmapEffectCollection.cs
- IPGlobalProperties.cs
- InputLangChangeEvent.cs
- KoreanLunisolarCalendar.cs
- ObjectMemberMapping.cs
- HasCopySemanticsAttribute.cs
- StylusPointPropertyId.cs
- VBCodeProvider.cs
- PreDigestedSignedInfo.cs
- UpdateProgress.cs
- URI.cs
- CompilationLock.cs
- ConfigUtil.cs
- WorkerRequest.cs
- StandardOleMarshalObject.cs
- _CookieModule.cs
- Triplet.cs
- AuthenticationService.cs
- BindableAttribute.cs
- InfocardExtendedInformationCollection.cs
- SqlXmlStorage.cs
- ZipIOLocalFileBlock.cs
- FormViewDeletedEventArgs.cs
- FrameworkContentElement.cs
- DataTableExtensions.cs
- RemotingSurrogateSelector.cs
- Context.cs
- XmlEntity.cs
- BamlStream.cs
- NavigatingCancelEventArgs.cs
- XmlReflectionMember.cs
- ViewgenContext.cs
- MediaElement.cs
- XmlSchemaResource.cs
- XPathNode.cs
- UrlPath.cs
- Parser.cs
- WmlTextBoxAdapter.cs
- COAUTHIDENTITY.cs
- ServicePointManager.cs
- StringFunctions.cs
- __FastResourceComparer.cs
- FixUpCollection.cs
- DesignColumnCollection.cs
- SiteMapNodeCollection.cs
- WebReferencesBuildProvider.cs
- EdmTypeAttribute.cs
- VersionUtil.cs
- DeferredTextReference.cs
- XPathParser.cs