Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / untmp / whidbey / QFE / ndp / clr / src / BCL / System / Security / Principal / WindowsPrincipal.cs / 1 / WindowsPrincipal.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // // WindowsPrincipal.cs // // Group membership checks. // namespace System.Security.Principal { using Microsoft.Win32; using Microsoft.Win32.SafeHandles; using System.Runtime.InteropServices; using System.Security.Permissions; using Hashtable = System.Collections.Hashtable; [Serializable] [ComVisible(true)] public enum WindowsBuiltInRole { Administrator = 0x220, User = 0x221, Guest = 0x222, PowerUser = 0x223, AccountOperator = 0x224, SystemOperator = 0x225, PrintOperator = 0x226, BackupOperator = 0x227, Replicator = 0x228 } [Serializable()] [HostProtection(SecurityInfrastructure=true)] [ComVisible(true)] public class WindowsPrincipal : IPrincipal { private WindowsIdentity m_identity = null; // Following 3 fields are present purely for serialization compatability with Everett: not used in Whidbey #pragma warning disable 169 private String[] m_roles; private Hashtable m_rolesTable; private bool m_rolesLoaded; #pragma warning restore 169 // // Constructors. // private WindowsPrincipal () {} public WindowsPrincipal (WindowsIdentity ntIdentity) { if (ntIdentity == null) throw new ArgumentNullException("ntIdentity"); m_identity = ntIdentity; } // // Properties. // public virtual IIdentity Identity { get { return m_identity; } } // // Public methods. // public virtual bool IsInRole (string role) { if (role == null || role.Length == 0) return false; NTAccount ntAccount = new NTAccount(role); IdentityReferenceCollection source = new IdentityReferenceCollection(1); source.Add(ntAccount); IdentityReferenceCollection target = NTAccount.Translate(source, typeof(SecurityIdentifier), false); SecurityIdentifier sid = target[0] as SecurityIdentifier; if (sid == null) return false; return IsInRole(sid); } public virtual bool IsInRole (WindowsBuiltInRole role) { if (role < WindowsBuiltInRole.Administrator || role > WindowsBuiltInRole.Replicator) throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)role), "role"); return IsInRole((int) role); } public virtual bool IsInRole (int rid) { SecurityIdentifier sid = new SecurityIdentifier(IdentifierAuthority.NTAuthority, new int[] {Win32Native.SECURITY_BUILTIN_DOMAIN_RID, rid}); return IsInRole(sid); } // This methods (with a SID parameter) is more general than the 2 overloads that accept a WindowsBuiltInRole or // a rid (as an int). It is also better from a performance standpoint than the overload that accepts a string. // The aformentioned overloads remain in this class since we do not want to introduce a // breaking change. However, this method should be used in all new applications and we should document this. [ComVisible(false)] public virtual bool IsInRole (SecurityIdentifier sid) { if (sid == null) throw new ArgumentNullException("sid"); // special case the anonymous identity. if (m_identity.TokenHandle.IsInvalid) return false; // CheckTokenMembership expects an impersonation token SafeTokenHandle token = SafeTokenHandle.InvalidHandle; if (m_identity.ImpersonationLevel == TokenImpersonationLevel.None) { if (!Win32Native.DuplicateTokenEx(m_identity.TokenHandle, (uint) TokenAccessLevels.Query, IntPtr.Zero, (uint) TokenImpersonationLevel.Identification, (uint) TokenType.TokenImpersonation, ref token)) throw new SecurityException(Win32Native.GetMessage(Marshal.GetLastWin32Error())); } bool isMember = false; // CheckTokenMembership will if (!Win32Native.CheckTokenMembership((m_identity.ImpersonationLevel != TokenImpersonationLevel.None ? m_identity.TokenHandle : token), sid.BinaryForm, ref isMember)) throw new SecurityException(Win32Native.GetMessage(Marshal.GetLastWin32Error())); token.Dispose(); return isMember; } } } // 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
- MatrixCamera.cs
- NullReferenceException.cs
- DisplayClaim.cs
- Image.cs
- XmlCustomFormatter.cs
- AnnotationResourceCollection.cs
- EdmEntityTypeAttribute.cs
- DataBindEngine.cs
- XmlObjectSerializerContext.cs
- RequestCachePolicyConverter.cs
- CodeSnippetStatement.cs
- TreeViewBindingsEditor.cs
- Crypto.cs
- CompoundFileStorageReference.cs
- MobileControlsSectionHelper.cs
- GeneralTransform3DGroup.cs
- TextServicesProperty.cs
- DbModificationCommandTree.cs
- XmlParser.cs
- KeyPullup.cs
- SupportingTokenChannel.cs
- HttpClientCredentialType.cs
- LogFlushAsyncResult.cs
- FileLevelControlBuilderAttribute.cs
- SqlUtil.cs
- ComponentDispatcher.cs
- FrameworkTextComposition.cs
- ExpandSegment.cs
- IPCCacheManager.cs
- EntityDataSourceContextDisposingEventArgs.cs
- HandleValueEditor.cs
- ToolStripSeparator.cs
- ClientOptions.cs
- wgx_render.cs
- XmlTextReaderImpl.cs
- SoapHttpTransportImporter.cs
- InvariantComparer.cs
- ToolStripContentPanelRenderEventArgs.cs
- CompilerState.cs
- SqlConnectionPoolProviderInfo.cs
- ConsumerConnectionPointCollection.cs
- AppDomainAttributes.cs
- InProcStateClientManager.cs
- WebHttpElement.cs
- SecurityManager.cs
- AudioFileOut.cs
- ActivityIdHeader.cs
- GridView.cs
- AdornerHitTestResult.cs
- TextRangeEdit.cs
- ColorConvertedBitmap.cs
- LongCountAggregationOperator.cs
- UriTemplatePathSegment.cs
- WorkflowOperationBehavior.cs
- BufferedGraphicsContext.cs
- ImageField.cs
- ErrorWebPart.cs
- SymLanguageVendor.cs
- XamlTreeBuilder.cs
- LocatorGroup.cs
- DeleteIndexBinder.cs
- ItemChangedEventArgs.cs
- SqlBuffer.cs
- RSACryptoServiceProvider.cs
- WebPartConnectionsEventArgs.cs
- UriExt.cs
- GestureRecognitionResult.cs
- CommandManager.cs
- XPathDocument.cs
- PowerEase.cs
- Internal.cs
- BinaryFormatterWriter.cs
- xsdvalidator.cs
- VisualStyleRenderer.cs
- DynamicPropertyHolder.cs
- RightsManagementPermission.cs
- Catch.cs
- XmlWellformedWriter.cs
- FixedDSBuilder.cs
- InheritanceContextHelper.cs
- backend.cs
- OleDbConnectionPoolGroupProviderInfo.cs
- ReferentialConstraint.cs
- ElementProxy.cs
- CfgArc.cs
- SchemaTableColumn.cs
- ProfessionalColors.cs
- BamlVersionHeader.cs
- DataGridViewDataConnection.cs
- StringStorage.cs
- DataGridItemAutomationPeer.cs
- PerformanceCounters.cs
- Decimal.cs
- DispatcherObject.cs
- DataPager.cs
- HtmlTableCellCollection.cs
- MessageDecoder.cs
- DomainConstraint.cs
- SimpleWorkerRequest.cs
- SqlUdtInfo.cs