18 private enum SerializationMask
32 private byte[] m_userSerializationData;
35 private const string PreFix =
"System.Security.ClaimsIdentity.";
38 private const string ActorKey =
"System.Security.ClaimsIdentity.actor";
41 private const string AuthenticationTypeKey =
"System.Security.ClaimsIdentity.authenticationType";
44 private const string BootstrapContextKey =
"System.Security.ClaimsIdentity.bootstrapContext";
47 private const string ClaimsKey =
"System.Security.ClaimsIdentity.claims";
50 private const string LabelKey =
"System.Security.ClaimsIdentity.label";
53 private const string NameClaimTypeKey =
"System.Security.ClaimsIdentity.nameClaimType";
56 private const string RoleClaimTypeKey =
"System.Security.ClaimsIdentity.roleClaimType";
59 private const string VersionKey =
"System.Security.ClaimsIdentity.version";
80 private string m_nameType =
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
83 private string m_roleType =
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
85 [OptionalField(VersionAdded = 2)]
86 private string m_version =
"1.0";
88 [OptionalField(VersionAdded = 2)]
91 [OptionalField(VersionAdded = 2)]
92 private string m_authenticationType;
94 [OptionalField(VersionAdded = 2)]
95 private object m_bootstrapContext;
97 [OptionalField(VersionAdded = 2)]
98 private string m_label;
100 [OptionalField(VersionAdded = 2)]
101 private string m_serializedNameType;
103 [OptionalField(VersionAdded = 2)]
104 private string m_serializedRoleType;
106 [OptionalField(VersionAdded = 2)]
107 private string m_serializedClaims;
129 if (value !=
null && IsCircular(value))
143 return m_bootstrapContext;
148 m_bootstrapContext = value;
158 for (
int j = 0; j < m_instanceClaims.
Count; j++)
160 yield
return m_instanceClaims[j];
162 if (m_externalClaims ==
null)
166 for (
int i = 0; i < m_externalClaims.
Count; i++)
168 if (m_externalClaims[i] !=
null)
170 foreach (
Claim item
in m_externalClaims[i])
184 [FriendAccessAllowed]
187 return m_externalClaims;
226 : this(identity, null)
233 : this(null, claims, null, null, null)
240 : this(null, null, authenticationType, null, null)
248 : this(null, claims, authenticationType, null, null)
256 : this(identity, claims, null, null, null)
264 public ClaimsIdentity(
string authenticationType,
string nameType,
string roleType)
265 : this(null, null, authenticationType, nameType, roleType)
275 : this(null, claims, authenticationType, nameType, roleType)
286 : this(identity, claims, authenticationType, nameType, roleType, checkAuthType: true)
294 if (checkAuthType && identity !=
null &&
string.IsNullOrEmpty(authenticationType))
304 m_authenticationType =
null;
314 m_authenticationType = authenticationType;
316 if (!
string.IsNullOrEmpty(nameType))
318 m_nameType = nameType;
321 if (!
string.IsNullOrEmpty(roleType))
323 m_roleType = roleType;
327 if (claimsIdentity !=
null)
329 m_label = claimsIdentity.m_label;
332 m_nameType = claimsIdentity.m_nameType;
336 m_roleType = claimsIdentity.m_roleType;
338 m_bootstrapContext = claimsIdentity.m_bootstrapContext;
339 if (claimsIdentity.Actor !=
null)
341 if (IsCircular(claimsIdentity.Actor))
343 throw new InvalidOperationException(Environment.GetResourceString(
"InvalidOperationException_ActorGraphCircular"));
345 if (!AppContextSwitches.SetActorAsReferenceWhenCopyingClaimsIdentity)
351 m_actor = claimsIdentity.
Actor;
356 SafeAddClaims(claimsIdentity.Claims);
360 SafeAddClaims(claimsIdentity.m_instanceClaims);
362 if (claimsIdentity.m_userSerializationData !=
null)
364 m_userSerializationData = (claimsIdentity.m_userSerializationData.Clone() as
byte[]);
367 else if (identity !=
null && !
string.IsNullOrEmpty(identity.
Name))
369 SafeAddClaim(
new Claim(m_nameType, identity.
Name,
"http://www.w3.org/2001/XMLSchema#string",
"LOCAL AUTHORITY",
"LOCAL AUTHORITY",
this));
373 SafeAddClaims(claims);
396 if (other.m_actor !=
null)
398 m_actor = other.m_actor.
Clone();
400 m_authenticationType = other.m_authenticationType;
401 m_bootstrapContext = other.m_bootstrapContext;
402 m_label = other.m_label;
403 m_nameType = other.m_nameType;
404 m_roleType = other.m_roleType;
405 if (other.m_userSerializationData !=
null)
407 m_userSerializationData = (other.m_userSerializationData.Clone() as
byte[]);
409 SafeAddClaims(other.m_instanceClaims);
424 Deserialize(info, context, useContext:
true);
444 claimsIdentity.m_authenticationType = m_authenticationType;
445 claimsIdentity.m_bootstrapContext = m_bootstrapContext;
446 claimsIdentity.m_label = m_label;
447 claimsIdentity.m_nameType = m_nameType;
448 claimsIdentity.m_roleType = m_roleType;
451 if (IsCircular(
Actor))
455 if (!AppContextSwitches.SetActorAsReferenceWhenCopyingClaimsIdentity)
464 return claimsIdentity;
480 m_instanceClaims.
Add(claim);
484 m_instanceClaims.
Add(claim.
Clone(
this));
499 foreach (
Claim claim
in claims)
516 for (
int i = 0; i < m_instanceClaims.
Count; i++)
518 if (m_instanceClaims[i] == claim)
540 [SecuritySafeCritical]
543 foreach (
Claim claim
in claims)
547 m_instanceClaims.
Add(claim);
551 m_instanceClaims.
Add(claim.
Clone(
this));
556 [SecuritySafeCritical]
557 private void SafeAddClaim(Claim claim)
559 if (claim.Subject ==
this)
561 m_instanceClaims.
Add(claim);
565 m_instanceClaims.
Add(claim.Clone(
this));
588 return list.AsReadOnly();
610 return list.AsReadOnly();
619 public virtual bool HasClaim(Predicate<Claim> match)
643 public virtual bool HasClaim(
string type,
string value)
711 m_serializedClaims = SerializeClaims();
712 m_serializedNameType = m_nameType;
713 m_serializedRoleType = m_roleType;
723 if (!
string.IsNullOrEmpty(m_serializedClaims))
725 DeserializeClaims(m_serializedClaims);
726 m_serializedClaims =
null;
728 m_nameType = (
string.IsNullOrEmpty(m_serializedNameType) ?
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name" : m_serializedNameType);
729 m_roleType = (
string.IsNullOrEmpty(m_serializedRoleType) ?
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role" : m_serializedRoleType);
757 info.AddValue(
"System.Security.ClaimsIdentity.version", m_version);
758 if (!
string.IsNullOrEmpty(m_authenticationType))
760 info.AddValue(
"System.Security.ClaimsIdentity.authenticationType", m_authenticationType);
762 info.AddValue(
"System.Security.ClaimsIdentity.nameClaimType", m_nameType);
763 info.AddValue(
"System.Security.ClaimsIdentity.roleClaimType", m_roleType);
764 if (!
string.IsNullOrEmpty(m_label))
766 info.AddValue(
"System.Security.ClaimsIdentity.label", m_label);
772 binaryFormatter.
Serialize(memoryStream, m_actor,
null, fCheck:
false);
776 info.AddValue(
"System.Security.ClaimsIdentity.claims", SerializeClaims());
777 if (m_bootstrapContext !=
null)
781 binaryFormatter.
Serialize(memoryStream2, m_bootstrapContext,
null, fCheck:
false);
788 private void DeserializeClaims(
string serializedClaims)
790 if (!
string.IsNullOrEmpty(serializedClaims))
795 for (
int i = 0; i < m_instanceClaims.
Count; i++)
797 m_instanceClaims[i].Subject =
this;
801 if (m_instanceClaims ==
null)
808 private string SerializeClaims()
824 while (claimsIdentity.Actor !=
null)
826 if (
this == claimsIdentity.Actor)
830 claimsIdentity = claimsIdentity.Actor;
839 throw new ArgumentNullException(
"reader");
841 SerializationMask serializationMask = (SerializationMask)reader.
ReadInt32();
842 if ((serializationMask & SerializationMask.AuthenticationType) == SerializationMask.AuthenticationType)
846 if ((serializationMask & SerializationMask.BootstrapConext) == SerializationMask.BootstrapConext)
850 if ((serializationMask & SerializationMask.NameClaimType) == SerializationMask.NameClaimType)
856 m_nameType =
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
858 if ((serializationMask & SerializationMask.RoleClaimType) == SerializationMask.RoleClaimType)
864 m_roleType =
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
866 if ((serializationMask & SerializationMask.HasClaims) == SerializationMask.HasClaims)
869 for (
int i = 0; i < num; i++)
871 Claim item =
new Claim(reader,
this);
872 m_instanceClaims.
Add(item);
885 return new Claim(reader,
this);
905 SerializationMask serializationMask = SerializationMask.None;
906 if (m_authenticationType !=
null)
908 serializationMask |= SerializationMask.AuthenticationType;
911 if (m_bootstrapContext !=
null)
913 string text = m_bootstrapContext as string;
916 serializationMask |= SerializationMask.BootstrapConext;
920 if (!
string.Equals(m_nameType,
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
StringComparison.Ordinal))
922 serializationMask |= SerializationMask.NameClaimType;
925 if (!
string.Equals(m_roleType,
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
StringComparison.Ordinal))
927 serializationMask |= SerializationMask.RoleClaimType;
930 if (!
string.IsNullOrWhiteSpace(m_label))
932 serializationMask |= SerializationMask.HasLabel;
935 if (m_instanceClaims.
Count > 0)
937 serializationMask |= SerializationMask.HasClaims;
942 serializationMask |= SerializationMask.Actor;
945 if (userData !=
null && userData.Length != 0)
948 serializationMask |= SerializationMask.UserData;
950 writer.
Write((
int)serializationMask);
952 if ((serializationMask & SerializationMask.AuthenticationType) == SerializationMask.AuthenticationType)
954 writer.
Write(m_authenticationType);
956 if ((serializationMask & SerializationMask.BootstrapConext) == SerializationMask.BootstrapConext)
958 writer.
Write(m_bootstrapContext as
string);
960 if ((serializationMask & SerializationMask.NameClaimType) == SerializationMask.NameClaimType)
962 writer.
Write(m_nameType);
964 if ((serializationMask & SerializationMask.RoleClaimType) == SerializationMask.RoleClaimType)
966 writer.
Write(m_roleType);
968 if ((serializationMask & SerializationMask.HasLabel) == SerializationMask.HasLabel)
970 writer.
Write(m_label);
972 if ((serializationMask & SerializationMask.HasClaims) == SerializationMask.HasClaims)
975 foreach (
Claim instanceClaim
in m_instanceClaims)
980 if ((serializationMask & SerializationMask.Actor) == SerializationMask.Actor)
984 if ((serializationMask & SerializationMask.UserData) == SerializationMask.UserData)
986 writer.
Write(userData.Length);
987 writer.
Write(userData);
1004 switch (enumerator.
Name)
1006 case "System.Security.ClaimsIdentity.version":
1008 string @
string = info.GetString(
"System.Security.ClaimsIdentity.version");
1011 case "System.Security.ClaimsIdentity.authenticationType":
1012 m_authenticationType = info.GetString(
"System.Security.ClaimsIdentity.authenticationType");
1014 case "System.Security.ClaimsIdentity.nameClaimType":
1015 m_nameType = info.GetString(
"System.Security.ClaimsIdentity.nameClaimType");
1017 case "System.Security.ClaimsIdentity.roleClaimType":
1018 m_roleType = info.GetString(
"System.Security.ClaimsIdentity.roleClaimType");
1020 case "System.Security.ClaimsIdentity.label":
1021 m_label = info.GetString(
"System.Security.ClaimsIdentity.label");
1023 case "System.Security.ClaimsIdentity.actor":
1029 case "System.Security.ClaimsIdentity.claims":
1030 DeserializeClaims(info.GetString(
"System.Security.ClaimsIdentity.claims"));
1032 case "System.Security.ClaimsIdentity.bootstrapContext":
1033 using (
MemoryStream serializationStream =
new MemoryStream(Convert.FromBase64String(info.GetString(
"System.Security.ClaimsIdentity.bootstrapContext"))))
1035 m_bootstrapContext = binaryFormatter.
Deserialize(serializationStream,
null, fCheck:
false);
Converts a base data type to another base data type.
ClaimsIdentity(ClaimsIdentity other)
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.
ClaimsIdentity(BinaryReader reader)
const string DefaultNameClaimType
The default name claim type; F:System.Security.Claims.ClaimTypes.Name.
string Label
Gets or sets the label for this claims identity.
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
Populates the T:System.Runtime.Serialization.SerializationInfo with data needed to serialize the curr...
ClaimsIdentity(string authenticationType)
Initializes a new instance of the T:System.Security.Claims.ClaimsIdentity class with an empty claims ...
ClaimsIdentity(string authenticationType, string nameType, string roleType)
Initializes a new instance of the T:System.Security.Claims.ClaimsIdentity class with the specified au...
int Count
Gets the number of elements contained in the T:System.Collections.Generic.List`1.
ClaimsIdentity(SerializationInfo info)
Initializes a new instance of the T:System.Security.Claims.ClaimsIdentity class from a serialized str...
virtual int ReadInt32()
Reads a 4-byte signed integer from the current stream and advances the current position of the stream...
string Type
Gets the claim type of the claim.
virtual void WriteTo(BinaryWriter writer)
void RemoveAt(int index)
Removes the element at the specified index of the T:System.Collections.Generic.List`1.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
virtual void Flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlyin...
No initialization action.
string NameClaimType
Gets the claim type that is used to determine which claims provide the value for the P:System....
ClaimsIdentity(IIdentity identity)
Initializes a new instance of the T:System.Security.Claims.ClaimsIdentity class using the name and au...
Describes the source and destination of a given serialized stream, and provides an additional caller-...
static string ToBase64String(byte[] inArray)
Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded ...
virtual bool TryRemoveClaim(Claim claim)
Attempts to remove a claim from the claims identity.
Represents a Windows user.
string Value
Gets the value of the claim.
virtual Claim CreateClaim(BinaryReader reader)
ClaimsIdentity(IIdentity identity, IEnumerable< Claim > claims, string authenticationType, string nameType, string roleType)
Initializes a new instance of the T:System.Security.Claims.ClaimsIdentity class from the specified T:...
virtual byte [] CustomSerializationData
virtual byte [] GetBuffer()
Returns the array of unsigned bytes from which this stream was created.
ClaimsIdentity(IEnumerable< Claim > claims)
Initializes a new instance of the T:System.Security.Claims.ClaimsIdentity class using an enumerated c...
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.
const string DefaultIssuer
The default issuer; “LOCAL AUTHORITY”.
Provides information about, and means to manipulate, the current environment and platform....
virtual Claim FindFirst(Predicate< Claim > match)
Retrieves the first claim that is matched by the specified predicate.
virtual void WriteTo(BinaryWriter writer, byte[] userData)
virtual void RemoveClaim(Claim claim)
Attempts to remove a claim from the claims identity.
Creates a stream whose backing store is memory.To browse the .NET Framework source code for this type...
virtual string Name
Gets the name of this claims identity.
A cast or conversion operation, such as (SampleType)obj in C::or CType(obj, SampleType) in Visual Bas...
const string DefaultRoleClaimType
The default role claim type; F:System.Security.Claims.ClaimTypes.Role.
Reads primitive data types as binary values in a specific encoding.
virtual IEnumerable< Claim > FindAll(string type)
Retrieves all of the claims that have the specified claim type.
virtual ClaimsIdentity Clone()
Returns a new T:System.Security.Claims.ClaimsIdentity copied from this claims identity.
virtual void WriteTo(BinaryWriter writer)
Represents a claims-based identity.
int Count
Gets the number of elements actually contained in the T:System.Collections.ObjectModel....
virtual void AddClaim(Claim claim)
Adds a single claim to this claims identity.
virtual void Write(bool value)
Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing t...
ClaimsIdentity(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.Security.Claims.ClaimsIdentity class from a serialized str...
ClaimsIdentity Subject
Gets the subject of the claim.
ClaimsIdentity Actor
Gets or sets the identity of the calling party that was granted delegation rights.
ClaimsIdentity()
Initializes a new instance of the T:System.Security.Claims.ClaimsIdentity class with an empty claims ...
ClaimsIdentity(IEnumerable< Claim > claims, string authenticationType, string nameType, string roleType)
Initializes a new instance of the T:System.Security.Claims.ClaimsIdentity class with the specified cl...
virtual bool HasClaim(string type, string value)
Determines whether this claims identity has a claim with the specified claim type and value.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
object BootstrapContext
Gets or sets the token that was used to create this claims identity.
virtual void AddClaims(IEnumerable< Claim > claims)
Adds a list of claims to this claims identity.
virtual bool IsAuthenticated
Gets a value that indicates whether the identity has been authenticated.
Allows an object to control its own serialization and deserialization.
string RoleClaimType
Gets the claim type that will be interpreted as a .NET Framework role among the claims in this claims...
string AuthenticationType
Gets the type of authentication used.
static unsafe byte [] FromBase64String(string s)
Converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit un...
ClaimsIdentity(IIdentity identity, IEnumerable< Claim > claims)
Initializes a new instance of the T:System.Security.Claims.ClaimsIdentity class using the specified c...
Specifies that the class can be serialized.
virtual Claim FindFirst(string type)
Retrieves the first claim with the specified claim type.
string Name
Gets the name for the item currently being examined.
The exception that is thrown when a method call is invalid for the object's current state.
Defines the basic functionality of an identity object.
virtual Claim Clone()
Returns a new T:System.Security.Claims.Claim object copied from this object. The new claim does not h...
ClaimsIdentity(IEnumerable< Claim > claims, string authenticationType)
Initializes a new instance of the T:System.Security.Claims.ClaimsIdentity class with the specified cl...
The exception that is thrown when the operating system denies access because of an I/O error or a spe...
string Name
Gets the name of the current user.
virtual string ReadString()
Reads a string from the current stream. The string is prefixed with the length, encoded as an integer...
Provides the base class for a generic collection.
virtual string AuthenticationType
Gets the authentication type.
virtual IEnumerable< Claim > FindAll(Predicate< Claim > match)
Retrieves all of the claims that are matched by the specified predicate.
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< Claim > Claims
Gets the claims associated with this claims identity.
bool MoveNext()
Updates the enumerator to the next item.
virtual bool HasClaim(Predicate< Claim > match)
Determines whether this claims identity has a claim that is matched by the specified predicate.