mscorlib(4.0.0.0) API with additions
WindowsPrincipal.cs
1 using Microsoft.Win32;
2 using Microsoft.Win32.SafeHandles;
3 using System.Collections;
9 
11 {
13  [Serializable]
14  [ComVisible(true)]
15  [HostProtection(SecurityAction.LinkDemand, SecurityInfrastructure = true)]
17  {
18  private WindowsIdentity m_identity;
19 
20  private string[] m_roles;
21 
22  private Hashtable m_rolesTable;
23 
24  private bool m_rolesLoaded;
25 
28  public override IIdentity Identity => m_identity;
29 
32  public virtual IEnumerable<Claim> UserClaims
33  {
34  get
35  {
36  foreach (ClaimsIdentity identity in Identities)
37  {
38  WindowsIdentity windowsIdentity = identity as WindowsIdentity;
39  if (windowsIdentity != null)
40  {
41  foreach (Claim userClaim in windowsIdentity.UserClaims)
42  {
43  yield return userClaim;
44  }
45  }
46  }
47  }
48  }
49 
52  public virtual IEnumerable<Claim> DeviceClaims
53  {
54  get
55  {
56  foreach (ClaimsIdentity identity in Identities)
57  {
58  WindowsIdentity windowsIdentity = identity as WindowsIdentity;
59  if (windowsIdentity != null)
60  {
61  foreach (Claim deviceClaim in windowsIdentity.DeviceClaims)
62  {
63  yield return deviceClaim;
64  }
65  }
66  }
67  }
68  }
69 
70  private WindowsPrincipal()
71  {
72  }
73 
78  public WindowsPrincipal(WindowsIdentity ntIdentity)
79  : base(ntIdentity)
80  {
81  if (ntIdentity == null)
82  {
83  throw new ArgumentNullException("ntIdentity");
84  }
85  m_identity = ntIdentity;
86  }
87 
88  [OnDeserialized]
89  [SecuritySafeCritical]
90  private void OnDeserializedMethod(StreamingContext context)
91  {
92  ClaimsIdentity claimsIdentity = null;
93  foreach (ClaimsIdentity identity in base.Identities)
94  {
95  if (identity != null)
96  {
97  claimsIdentity = identity;
98  break;
99  }
100  }
101  if (claimsIdentity == null)
102  {
103  base.AddIdentity(m_identity);
104  }
105  }
106 
111  [SecuritySafeCritical]
112  [SecurityPermission(SecurityAction.Demand, ControlPrincipal = true)]
113  public override bool IsInRole(string role)
114  {
115  if (role == null || role.Length == 0)
116  {
117  return false;
118  }
119  NTAccount identity = new NTAccount(role);
120  IdentityReferenceCollection identityReferenceCollection = new IdentityReferenceCollection(1);
121  identityReferenceCollection.Add(identity);
122  IdentityReferenceCollection identityReferenceCollection2 = NTAccount.Translate(identityReferenceCollection, typeof(SecurityIdentifier), forceSuccess: false);
123  SecurityIdentifier securityIdentifier = identityReferenceCollection2[0] as SecurityIdentifier;
124  if (securityIdentifier != null && IsInRole(securityIdentifier))
125  {
126  return true;
127  }
128  return base.IsInRole(role);
129  }
130 
137  public virtual bool IsInRole(WindowsBuiltInRole role)
138  {
139  if (role < WindowsBuiltInRole.Administrator || role > WindowsBuiltInRole.Replicator)
140  {
141  throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)role), "role");
142  }
143  return IsInRole((int)role);
144  }
145 
150  public virtual bool IsInRole(int rid)
151  {
152  SecurityIdentifier sid = new SecurityIdentifier(IdentifierAuthority.NTAuthority, new int[2]
153  {
154  32,
155  rid
156  });
157  return IsInRole(sid);
158  }
159 
167  [SecuritySafeCritical]
168  [ComVisible(false)]
169  public virtual bool IsInRole(SecurityIdentifier sid)
170  {
171  if (sid == null)
172  {
173  throw new ArgumentNullException("sid");
174  }
175  if (m_identity.AccessToken.IsInvalid)
176  {
177  return false;
178  }
179  SafeAccessTokenHandle phNewToken = SafeAccessTokenHandle.InvalidHandle;
180  if (m_identity.ImpersonationLevel == TokenImpersonationLevel.None && !Win32Native.DuplicateTokenEx(m_identity.AccessToken, 8u, IntPtr.Zero, 2u, 2u, ref phNewToken))
181  {
182  throw new SecurityException(Win32Native.GetMessage(Marshal.GetLastWin32Error()));
183  }
184  bool IsMember = false;
185  if (!Win32Native.CheckTokenMembership((m_identity.ImpersonationLevel != 0) ? m_identity.AccessToken : phNewToken, sid.BinaryForm, ref IsMember))
186  {
187  throw new SecurityException(Win32Native.GetMessage(Marshal.GetLastWin32Error()));
188  }
189  phNewToken.Dispose();
190  return IsMember;
191  }
192  }
193 }
Enables code to check the Windows group membership of a Windows user.
TokenImpersonationLevel ImpersonationLevel
Gets the impersonation level for the user.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
override bool IsInRole(string role)
Determines whether the current principal belongs to the Windows user group with the specified name.
void Add(IdentityReference identity)
Adds an T:System.Security.Principal.IdentityReference object to the T:System.Security....
Definition: __Canon.cs:3
TokenImpersonationLevel
Defines security impersonation levels. Security impersonation levels govern the degree to which a ser...
override IIdentity Identity
Gets the identity of the current principal.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Represents a user or group account.
Definition: NTAccount.cs:10
virtual IEnumerable< Claim > UserClaims
Gets claims that have the F:System.Security.Claims.ClaimTypes.WindowsUserClaim property key.
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
An T:System.Security.Principal.IPrincipal implementation that supports multiple claims-based identiti...
virtual IEnumerable< Claim > UserClaims
Gets all Windows user claims from this principal.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
WindowsPrincipal(WindowsIdentity ntIdentity)
Initializes a new instance of the T:System.Security.Principal.WindowsPrincipal class by using the spe...
Represents a claim.
Definition: Claim.cs:10
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
Represents a claims-based identity.
SafeAccessTokenHandle AccessToken
Gets this T:Microsoft.Win32.SafeHandles.SafeAccessTokenHandle for this T:System.Security....
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
Represents a collection of T:System.Security.Principal.IdentityReference objects and provides a means...
virtual IEnumerable< Claim > DeviceClaims
Gets claims that have the F:System.Security.Claims.ClaimTypes.WindowsDeviceClaim property key.
virtual IEnumerable< Claim > DeviceClaims
Gets all Windows device claims from this principal.
WindowsBuiltInRole
Specifies common roles to be used with M:System.Security.Principal.WindowsPrincipal....
The exception that is thrown when one of the arguments provided to a method is not valid.
virtual bool IsInRole(int rid)
Determines whether the current principal belongs to the Windows user group with the specified relativ...
Represents a security identifier (SID) and provides marshaling and comparison operations for SIDs.
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Definition: IntPtr.cs:20
virtual bool IsInRole(SecurityIdentifier sid)
Determines whether the current principal belongs to the Windows user group with the specified securit...
Specifies that the class can be serialized.
override IdentityReference Translate(Type targetType)
Translates the account name represented by the T:System.Security.Principal.NTAccount object into anot...
Definition: NTAccount.cs:111
Defines the basic functionality of an identity object.
Definition: IIdentity.cs:8
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
virtual bool IsInRole(WindowsBuiltInRole role)
Determines whether the current principal belongs to the Windows user group with the specified T:Syste...
virtual IEnumerable< ClaimsIdentity > Identities
Gets a collection that contains all of the claims identities associated with this claims principal.
The exception that is thrown when a security error is detected.