Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Security / Principal / WindowsPrincipal.cs / 1305376 / 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 System.Diagnostics.Contracts;
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");
Contract.EndContractBlock();
m_identity = ntIdentity;
}
//
// Properties.
//
public virtual IIdentity Identity {
get {
return m_identity;
}
}
//
// Public methods.
//
[SecuritySafeCritical]
[SecurityPermission(SecurityAction.Demand, ControlPrincipal = true)]
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");
Contract.EndContractBlock();
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.
[System.Security.SecuritySafeCritical] // auto-generated
[ComVisible(false)]
public virtual bool IsInRole (SecurityIdentifier sid) {
if (sid == null)
throw new ArgumentNullException("sid");
Contract.EndContractBlock();
// 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 check if the SID is both present and enabled in the access token.
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.
//
// ==--==
// [....]
//
//
// 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 System.Diagnostics.Contracts;
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");
Contract.EndContractBlock();
m_identity = ntIdentity;
}
//
// Properties.
//
public virtual IIdentity Identity {
get {
return m_identity;
}
}
//
// Public methods.
//
[SecuritySafeCritical]
[SecurityPermission(SecurityAction.Demand, ControlPrincipal = true)]
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");
Contract.EndContractBlock();
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.
[System.Security.SecuritySafeCritical] // auto-generated
[ComVisible(false)]
public virtual bool IsInRole (SecurityIdentifier sid) {
if (sid == null)
throw new ArgumentNullException("sid");
Contract.EndContractBlock();
// 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 check if the SID is both present and enabled in the access token.
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.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SafeFileMappingHandle.cs
- TriggerBase.cs
- SqlBulkCopyColumnMapping.cs
- WorkflowRuntimeServiceElementCollection.cs
- ExtendedPropertyDescriptor.cs
- LassoHelper.cs
- ProtectedConfigurationProviderCollection.cs
- Dictionary.cs
- VariableExpressionConverter.cs
- BooleanStorage.cs
- _AutoWebProxyScriptHelper.cs
- CustomAttribute.cs
- XmlTextReader.cs
- MatchingStyle.cs
- linebase.cs
- TokenBasedSet.cs
- Compress.cs
- Win32MouseDevice.cs
- Odbc32.cs
- mactripleDES.cs
- AxHostDesigner.cs
- SerializationStore.cs
- Model3DGroup.cs
- MergeFailedEvent.cs
- BitmapEffectDrawingContextState.cs
- DataGridViewCellParsingEventArgs.cs
- DecimalStorage.cs
- MemoryMappedView.cs
- BitArray.cs
- TextDecorationUnitValidation.cs
- DataGridViewRowHeightInfoNeededEventArgs.cs
- ServiceContractDetailViewControl.cs
- MonthChangedEventArgs.cs
- WebPartHeaderCloseVerb.cs
- BitmapVisualManager.cs
- HMACSHA1.cs
- HttpRuntime.cs
- TracedNativeMethods.cs
- LookupBindingPropertiesAttribute.cs
- ResourceReferenceKeyNotFoundException.cs
- RelationshipFixer.cs
- Metadata.cs
- StoreAnnotationsMap.cs
- XmlComplianceUtil.cs
- EntityDataSourceSelectedEventArgs.cs
- SchemaTableColumn.cs
- ClientSettingsSection.cs
- ZipIOModeEnforcingStream.cs
- SolidColorBrush.cs
- AnchoredBlock.cs
- SourceSwitch.cs
- CacheEntry.cs
- SecurityElement.cs
- smtpconnection.cs
- KeySpline.cs
- ParsedAttributeCollection.cs
- EventlogProvider.cs
- LinqDataSourceHelper.cs
- WarningException.cs
- XomlCompiler.cs
- DbConnectionPoolOptions.cs
- TrackingServices.cs
- PageAdapter.cs
- MenuItemStyleCollection.cs
- _AcceptOverlappedAsyncResult.cs
- ArraySet.cs
- QuerySelectOp.cs
- HttpConfigurationSystem.cs
- Converter.cs
- PolicyException.cs
- ItemCollection.cs
- VisualBasicDesignerHelper.cs
- ConfigurationProperty.cs
- ComponentCollection.cs
- SpotLight.cs
- WaitForChangedResult.cs
- sqlser.cs
- WindowsToolbar.cs
- CryptographicAttribute.cs
- PagePropertiesChangingEventArgs.cs
- IdentityModelDictionary.cs
- AssemblyName.cs
- TableLayoutRowStyleCollection.cs
- MethodBody.cs
- Regex.cs
- BuildDependencySet.cs
- XmlArrayAttribute.cs
- UpWmlMobileTextWriter.cs
- CursorInteropHelper.cs
- OperationExecutionFault.cs
- ObjectListCommand.cs
- RtfControlWordInfo.cs
- SafeThemeHandle.cs
- InputLanguageSource.cs
- TextRunCacheImp.cs
- Page.cs
- QilFactory.cs
- InvalidAsynchronousStateException.cs
- SupportsEventValidationAttribute.cs
- UriTemplateQueryValue.cs