18 private enum SerializationMask
26 private byte[] m_userSerializationData;
29 private const string PreFix =
"System.Security.ClaimsPrincipal.";
32 private const string IdentitiesKey =
"System.Security.ClaimsPrincipal.Identities";
35 private const string VersionKey =
"System.Security.ClaimsPrincipal.Version";
37 [OptionalField(VersionAdded = 2)]
38 private string m_version =
"1.0";
40 [OptionalField(VersionAdded = 2)]
41 private string m_serializedClaimsIdentities;
47 private static Func<IEnumerable<ClaimsIdentity>,
ClaimsIdentity> s_identitySelector = SelectPrimaryIdentity;
58 return s_identitySelector;
63 s_identitySelector = value;
73 return s_principalSelector;
78 s_principalSelector = value;
107 if (s_principalSelector !=
null)
109 return s_principalSelector();
111 return SelectClaimsPrincipal();
125 if (s_identitySelector !=
null)
127 return s_identitySelector(m_identities);
129 return SelectPrimaryIdentity(m_identities);
135 if (identities ==
null)
139 ClaimsIdentity claimsIdentity =
null;
140 foreach (ClaimsIdentity identity
in identities)
146 if (claimsIdentity ==
null)
148 claimsIdentity = identity;
151 return claimsIdentity;
157 if (claimsPrincipal !=
null)
159 return claimsPrincipal;
175 if (identities ==
null)
188 if (identity ==
null)
193 if (claimsIdentity !=
null)
195 m_identities.
Add(claimsIdentity);
209 if (principal ==
null)
214 if (claimsPrincipal ==
null)
247 Deserialize(info, context);
275 m_serializedClaimsIdentities = SerializeIdentities();
285 DeserializeIdentities(m_serializedClaimsIdentities);
286 m_serializedClaimsIdentities =
null;
303 info.AddValue(
"System.Security.ClaimsPrincipal.Identities", SerializeIdentities());
304 info.AddValue(
"System.Security.ClaimsPrincipal.Version", m_version);
318 string name = enumerator.
Name;
319 if (!(name ==
"System.Security.ClaimsPrincipal.Identities"))
321 if (name ==
"System.Security.ClaimsPrincipal.Version")
323 m_version = info.GetString(
"System.Security.ClaimsPrincipal.Version");
328 DeserializeIdentities(info.GetString(
"System.Security.ClaimsPrincipal.Identities"));
334 private void DeserializeIdentities(
string identities)
337 if (!
string.IsNullOrEmpty(identities))
347 if (num >= list.Count)
351 ClaimsIdentity claimsIdentity =
null;
354 claimsIdentity = (ClaimsIdentity)binaryFormatter.
Deserialize(serializationStream2,
null, fCheck:
false);
356 if (!
string.IsNullOrEmpty(list[num]))
362 claimsIdentity =
new WindowsIdentity(claimsIdentity,
new IntPtr(result));
364 m_identities.
Add(claimsIdentity);
373 private string SerializeIdentities()
377 foreach (ClaimsIdentity identity
in m_identities)
385 binaryFormatter.
Serialize(memoryStream, windowsIdentity.CloneAsBase(),
null, fCheck:
false);
394 binaryFormatter.
Serialize(memoryStream2, identity,
null, fCheck:
false);
401 binaryFormatter.
Serialize(memoryStream3, list,
null, fCheck:
false);
413 if (identity ==
null)
417 m_identities.Add(identity);
427 if (identities ==
null)
431 m_identities.AddRange(identities);
448 if (identity !=
null)
456 return list.AsReadOnly();
473 if (identity !=
null)
481 return list.AsReadOnly();
498 if (identity !=
null)
522 for (
int i = 0; i < m_identities.Count; i++)
524 if (m_identities[i] !=
null)
526 claim = m_identities[i].FindFirst(type);
542 public virtual bool HasClaim(Predicate<Claim> match)
548 for (
int i = 0; i < m_identities.Count; i++)
550 if (m_identities[i] !=
null && m_identities[i].
HasClaim(match))
566 public virtual bool HasClaim(
string type,
string value)
576 for (
int i = 0; i < m_identities.Count; i++)
578 if (m_identities[i] !=
null && m_identities[i].
HasClaim(type, value))
592 for (
int i = 0; i < m_identities.Count; i++)
594 if (m_identities[i] !=
null && m_identities[i].
HasClaim(m_identities[i].RoleClaimType, role))
608 SerializationMask serializationMask = (SerializationMask)reader.
ReadInt32();
611 if ((serializationMask & SerializationMask.HasIdentities) == SerializationMask.HasIdentities)
615 for (
int i = 0; i < num3; i++)
620 if ((serializationMask & SerializationMask.UserData) == SerializationMask.UserData)
623 m_userSerializationData = reader.
ReadBytes(count);
626 for (
int j = num2; j < num; j++)
649 SerializationMask serializationMask = SerializationMask.None;
650 if (m_identities.Count > 0)
652 serializationMask |= SerializationMask.HasIdentities;
655 if (userData !=
null && userData.Length != 0)
658 serializationMask |= SerializationMask.UserData;
660 writer.
Write((
int)serializationMask);
662 if ((serializationMask & SerializationMask.HasIdentities) == SerializationMask.HasIdentities)
664 writer.
Write(m_identities.Count);
670 if ((serializationMask & SerializationMask.UserData) == SerializationMask.UserData)
672 writer.
Write(userData.Length);
673 writer.
Write(userData);
static IPrincipal CurrentPrincipal
Gets or sets the thread's current principal (for role-based security).
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 long Length
Gets the length of the stream in bytes.
virtual void WriteTo(BinaryWriter writer, byte[] userData)
static Func< ClaimsPrincipal > ClaimsPrincipalSelector
Gets and sets the delegate used to select the claims principal returned by the P:System....
virtual int ReadInt32()
Reads a 4-byte signed integer from the current stream and advances the current position of the stream...
virtual void AddIdentities(IEnumerable< ClaimsIdentity > identities)
Adds the specified claims identities to this claims principal.
virtual void Flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlyin...
Defines the basic functionality of a principal object.
No initialization action.
ReadOnlyCollection< T > AsReadOnly()
Returns a read-only T:System.Collections.ObjectModel.ReadOnlyCollection`1 wrapper for the current col...
static Func< IEnumerable< ClaimsIdentity >, ClaimsIdentity > PrimaryIdentitySelector
Gets and sets the delegate used to select the claims identity returned by the P:System....
ClaimsPrincipal(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.Security.Claims.ClaimsPrincipal class from a serialized st...
unsafe long ToInt64()
Converts the value of this instance to a 64-bit signed integer.
virtual IEnumerable< Claim > FindAll(string type)
Retrieves all or the claims that have the specified claim type.
ClaimsPrincipal(BinaryReader reader)
Describes the source and destination of a given serialized stream, and provides an additional caller-...
virtual IIdentity Identity
Gets the primary claims identity associated with this claims principal.
virtual IEnumerable< Claim > FindAll(Predicate< Claim > match)
Retrieves all of the claims that are matched by the specified predicate.
virtual bool HasClaim(string type, string value)
Determines whether any of the claims identities associated with this claims principal contains a clai...
virtual ClaimsIdentity CreateClaimsIdentity(BinaryReader reader)
Creates a new claims identity.
NumberStyles
Determines the styles permitted in numeric string arguments that are passed to the Parse and TryParse...
Represents a Windows user.
virtual byte [] GetBuffer()
Returns the array of unsigned bytes from which this stream was created.
virtual bool HasClaim(Predicate< Claim > match)
Determines whether any of the claims identities associated with this claims principal contains a clai...
void Add(T item)
Adds an object to the end of the T:System.Collections.Generic.List`1.
SecurityAction
Specifies the security actions that can be performed using declarative security.
virtual bool IsInRole(string role)
Returns a value that indicates whether the entity (user) represented by this claims principal is in t...
virtual Claim FindFirst(Predicate< Claim > match)
Retrieves the first claim that is matched by the specified predicate.
virtual byte [] CustomSerializationData
IIdentity Identity
Gets the identity of the current principal.
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
Populates the T:System.Runtime.Serialization.SerializationInfo with data needed to serialize the curr...
Creates a stream whose backing store is memory.To browse the .NET Framework source code for this type...
An T:System.Security.Principal.IPrincipal implementation that supports multiple claims-based identiti...
virtual IEnumerable< Claim > Claims
Gets a collection that contains all of the claims from all of the claims identities associated with t...
A cast or conversion operation, such as (SampleType)obj in C::or CType(obj, SampleType) in Visual Bas...
ClaimsPrincipal()
Initializes a new instance of the T:System.Security.Claims.ClaimsPrincipal class.
virtual byte [] ReadBytes(int count)
Reads the specified number of bytes from the current stream into a byte array and advances the curren...
Reads primitive data types as binary values in a specific encoding.
virtual void WriteTo(BinaryWriter writer)
virtual ClaimsPrincipal Clone()
Returns a copy of this instance.
Represents a claims-based identity.
The exception thrown when an error occurs during serialization or deserialization.
virtual void Write(bool value)
Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing t...
void AddRange(IEnumerable< T > collection)
Adds the elements of the specified collection to the end of the T:System.Collections....
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
virtual Claim FindFirst(string type)
Retrieves the first claim with the specified claim type.
ClaimsPrincipal(IEnumerable< ClaimsIdentity > identities)
Initializes a new instance of the T:System.Security.Claims.ClaimsPrincipal class using the specified ...
virtual void WriteTo(BinaryWriter writer)
Allows an object to control its own serialization and deserialization.
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Specifies that the class can be serialized.
string Name
Gets the name for the item currently being examined.
Defines the basic functionality of an identity object.
static ClaimsPrincipal Current
Gets the current claims principal.
virtual string ReadString()
Reads a string from the current stream. The string is prefixed with the length, encoded as an integer...
virtual Claim FindFirst(Predicate< Claim > match)
Retrieves the first claim that is matched by the specified predicate.
virtual IEnumerable< Claim > FindAll(Predicate< Claim > match)
Retrieves all of the claims that are matched by the specified predicate.
virtual void AddIdentity(ClaimsIdentity identity)
Adds the specified claims identity to this claims principal.
Writes primitive types in binary to a stream and supports writing strings in a specific encoding.
Provides a formatter-friendly mechanism for parsing the data in T:System.Runtime.Serialization....
virtual IEnumerable< ClaimsIdentity > Identities
Gets a collection that contains all of the claims identities associated with this claims principal.
ClaimsPrincipal(IIdentity identity)
Initializes a new instance of the T:System.Security.Claims.ClaimsPrincipal class from the specified i...
virtual IEnumerable< Claim > Claims
Gets the claims associated with this claims identity.
bool MoveNext()
Updates the enumerator to the next item.
ClaimsPrincipal(IPrincipal principal)
Initializes a new instance of the T:System.Security.Claims.ClaimsPrincipal class from the specified p...
Creates and controls a thread, sets its priority, and gets its status.