mscorlib(4.0.0.0) API with additions
ClaimsIdentity.cs
3 using System.IO;
10 
11 namespace System.Security.Claims
12 {
14  [Serializable]
15  [ComVisible(true)]
16  public class ClaimsIdentity : IIdentity
17  {
18  private enum SerializationMask
19  {
20  None = 0,
22  BootstrapConext = 2,
23  NameClaimType = 4,
24  RoleClaimType = 8,
25  HasClaims = 0x10,
26  HasLabel = 0x20,
27  Actor = 0x40,
28  UserData = 0x80
29  }
30 
31  [NonSerialized]
32  private byte[] m_userSerializationData;
33 
34  [NonSerialized]
35  private const string PreFix = "System.Security.ClaimsIdentity.";
36 
37  [NonSerialized]
38  private const string ActorKey = "System.Security.ClaimsIdentity.actor";
39 
40  [NonSerialized]
41  private const string AuthenticationTypeKey = "System.Security.ClaimsIdentity.authenticationType";
42 
43  [NonSerialized]
44  private const string BootstrapContextKey = "System.Security.ClaimsIdentity.bootstrapContext";
45 
46  [NonSerialized]
47  private const string ClaimsKey = "System.Security.ClaimsIdentity.claims";
48 
49  [NonSerialized]
50  private const string LabelKey = "System.Security.ClaimsIdentity.label";
51 
52  [NonSerialized]
53  private const string NameClaimTypeKey = "System.Security.ClaimsIdentity.nameClaimType";
54 
55  [NonSerialized]
56  private const string RoleClaimTypeKey = "System.Security.ClaimsIdentity.roleClaimType";
57 
58  [NonSerialized]
59  private const string VersionKey = "System.Security.ClaimsIdentity.version";
60 
62  [NonSerialized]
63  public const string DefaultIssuer = "LOCAL AUTHORITY";
64 
66  [NonSerialized]
67  public const string DefaultNameClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
68 
70  [NonSerialized]
71  public const string DefaultRoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
72 
73  [NonSerialized]
74  private List<Claim> m_instanceClaims = new List<Claim>();
75 
76  [NonSerialized]
77  private Collection<IEnumerable<Claim>> m_externalClaims = new Collection<IEnumerable<Claim>>();
78 
79  [NonSerialized]
80  private string m_nameType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
81 
82  [NonSerialized]
83  private string m_roleType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
84 
85  [OptionalField(VersionAdded = 2)]
86  private string m_version = "1.0";
87 
88  [OptionalField(VersionAdded = 2)]
89  private ClaimsIdentity m_actor;
90 
91  [OptionalField(VersionAdded = 2)]
92  private string m_authenticationType;
93 
94  [OptionalField(VersionAdded = 2)]
95  private object m_bootstrapContext;
96 
97  [OptionalField(VersionAdded = 2)]
98  private string m_label;
99 
100  [OptionalField(VersionAdded = 2)]
101  private string m_serializedNameType;
102 
103  [OptionalField(VersionAdded = 2)]
104  private string m_serializedRoleType;
105 
106  [OptionalField(VersionAdded = 2)]
107  private string m_serializedClaims;
108 
111  public virtual string AuthenticationType => m_authenticationType;
112 
116  public virtual bool IsAuthenticated => !string.IsNullOrEmpty(m_authenticationType);
117 
121  public ClaimsIdentity Actor
122  {
123  get
124  {
125  return m_actor;
126  }
127  set
128  {
129  if (value != null && IsCircular(value))
130  {
131  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperationException_ActorGraphCircular"));
132  }
133  m_actor = value;
134  }
135  }
136 
139  public object BootstrapContext
140  {
141  get
142  {
143  return m_bootstrapContext;
144  }
145  [SecurityCritical]
146  set
147  {
148  m_bootstrapContext = value;
149  }
150  }
151 
154  public virtual IEnumerable<Claim> Claims
155  {
156  get
157  {
158  for (int j = 0; j < m_instanceClaims.Count; j++)
159  {
160  yield return m_instanceClaims[j];
161  }
162  if (m_externalClaims == null)
163  {
164  yield break;
165  }
166  for (int i = 0; i < m_externalClaims.Count; i++)
167  {
168  if (m_externalClaims[i] != null)
169  {
170  foreach (Claim item in m_externalClaims[i])
171  {
172  yield return item;
173  }
174  }
175  }
176  }
177  }
178 
180  protected virtual byte[] CustomSerializationData => m_userSerializationData;
181 
182  internal Collection<IEnumerable<Claim>> ExternalClaims
183  {
184  [FriendAccessAllowed]
185  get
186  {
187  return m_externalClaims;
188  }
189  }
190 
193  public string Label
194  {
195  get
196  {
197  return m_label;
198  }
199  set
200  {
201  m_label = value;
202  }
203  }
204 
207  public virtual string Name => FindFirst(m_nameType)?.Value;
208 
211  public string NameClaimType => m_nameType;
212 
215  public string RoleClaimType => m_roleType;
216 
218  public ClaimsIdentity()
219  : this((IEnumerable<Claim>)null)
220  {
221  }
222 
225  public ClaimsIdentity(IIdentity identity)
226  : this(identity, null)
227  {
228  }
229 
233  : this(null, claims, null, null, null)
234  {
235  }
236 
239  public ClaimsIdentity(string authenticationType)
240  : this(null, null, authenticationType, null, null)
241  {
242  }
243 
247  public ClaimsIdentity(IEnumerable<Claim> claims, string authenticationType)
248  : this(null, claims, authenticationType, null, null)
249  {
250  }
251 
255  public ClaimsIdentity(IIdentity identity, IEnumerable<Claim> claims)
256  : this(identity, claims, null, null, null)
257  {
258  }
259 
264  public ClaimsIdentity(string authenticationType, string nameType, string roleType)
265  : this(null, null, authenticationType, nameType, roleType)
266  {
267  }
268 
274  public ClaimsIdentity(IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType)
275  : this(null, claims, authenticationType, nameType, roleType)
276  {
277  }
278 
285  public ClaimsIdentity(IIdentity identity, IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType)
286  : this(identity, claims, authenticationType, nameType, roleType, checkAuthType: true)
287  {
288  }
289 
290  internal ClaimsIdentity(IIdentity identity, IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType, bool checkAuthType)
291  {
292  bool flag = false;
293  bool flag2 = false;
294  if (checkAuthType && identity != null && string.IsNullOrEmpty(authenticationType))
295  {
296  if (identity is WindowsIdentity)
297  {
298  try
299  {
300  m_authenticationType = identity.AuthenticationType;
301  }
303  {
304  m_authenticationType = null;
305  }
306  }
307  else
308  {
309  m_authenticationType = identity.AuthenticationType;
310  }
311  }
312  else
313  {
314  m_authenticationType = authenticationType;
315  }
316  if (!string.IsNullOrEmpty(nameType))
317  {
318  m_nameType = nameType;
319  flag = true;
320  }
321  if (!string.IsNullOrEmpty(roleType))
322  {
323  m_roleType = roleType;
324  flag2 = true;
325  }
326  ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;
327  if (claimsIdentity != null)
328  {
329  m_label = claimsIdentity.m_label;
330  if (!flag)
331  {
332  m_nameType = claimsIdentity.m_nameType;
333  }
334  if (!flag2)
335  {
336  m_roleType = claimsIdentity.m_roleType;
337  }
338  m_bootstrapContext = claimsIdentity.m_bootstrapContext;
339  if (claimsIdentity.Actor != null)
340  {
341  if (IsCircular(claimsIdentity.Actor))
342  {
343  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperationException_ActorGraphCircular"));
344  }
345  if (!AppContextSwitches.SetActorAsReferenceWhenCopyingClaimsIdentity)
346  {
347  m_actor = claimsIdentity.Actor.Clone();
348  }
349  else
350  {
351  m_actor = claimsIdentity.Actor;
352  }
353  }
354  if (claimsIdentity is WindowsIdentity && !(this is WindowsIdentity))
355  {
356  SafeAddClaims(claimsIdentity.Claims);
357  }
358  else
359  {
360  SafeAddClaims(claimsIdentity.m_instanceClaims);
361  }
362  if (claimsIdentity.m_userSerializationData != null)
363  {
364  m_userSerializationData = (claimsIdentity.m_userSerializationData.Clone() as byte[]);
365  }
366  }
367  else if (identity != null && !string.IsNullOrEmpty(identity.Name))
368  {
369  SafeAddClaim(new Claim(m_nameType, identity.Name, "http://www.w3.org/2001/XMLSchema#string", "LOCAL AUTHORITY", "LOCAL AUTHORITY", this));
370  }
371  if (claims != null)
372  {
373  SafeAddClaims(claims);
374  }
375  }
376 
380  {
381  if (reader == null)
382  {
383  throw new ArgumentNullException("reader");
384  }
385  Initialize(reader);
386  }
387 
391  {
392  if (other == null)
393  {
394  throw new ArgumentNullException("other");
395  }
396  if (other.m_actor != null)
397  {
398  m_actor = other.m_actor.Clone();
399  }
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)
406  {
407  m_userSerializationData = (other.m_userSerializationData.Clone() as byte[]);
408  }
409  SafeAddClaims(other.m_instanceClaims);
410  }
411 
417  [SecurityCritical]
419  {
420  if (info == null)
421  {
422  throw new ArgumentNullException("info");
423  }
424  Deserialize(info, context, useContext: true);
425  }
426 
429  [SecurityCritical]
431  {
432  if (info == null)
433  {
434  throw new ArgumentNullException("info");
435  }
436  Deserialize(info, default(StreamingContext), useContext: false);
437  }
438 
441  public virtual ClaimsIdentity Clone()
442  {
443  ClaimsIdentity claimsIdentity = new ClaimsIdentity(m_instanceClaims);
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;
449  if (Actor != null)
450  {
451  if (IsCircular(Actor))
452  {
453  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperationException_ActorGraphCircular"));
454  }
455  if (!AppContextSwitches.SetActorAsReferenceWhenCopyingClaimsIdentity)
456  {
457  claimsIdentity.Actor = Actor.Clone();
458  }
459  else
460  {
461  claimsIdentity.Actor = Actor;
462  }
463  }
464  return claimsIdentity;
465  }
466 
471  [SecurityCritical]
472  public virtual void AddClaim(Claim claim)
473  {
474  if (claim == null)
475  {
476  throw new ArgumentNullException("claim");
477  }
478  if (claim.Subject == this)
479  {
480  m_instanceClaims.Add(claim);
481  }
482  else
483  {
484  m_instanceClaims.Add(claim.Clone(this));
485  }
486  }
487 
492  [SecurityCritical]
493  public virtual void AddClaims(IEnumerable<Claim> claims)
494  {
495  if (claims == null)
496  {
497  throw new ArgumentNullException("claims");
498  }
499  foreach (Claim claim in claims)
500  {
501  if (claim != null)
502  {
503  AddClaim(claim);
504  }
505  }
506  }
507 
512  [SecurityCritical]
513  public virtual bool TryRemoveClaim(Claim claim)
514  {
515  bool result = false;
516  for (int i = 0; i < m_instanceClaims.Count; i++)
517  {
518  if (m_instanceClaims[i] == claim)
519  {
520  m_instanceClaims.RemoveAt(i);
521  result = true;
522  break;
523  }
524  }
525  return result;
526  }
527 
531  [SecurityCritical]
532  public virtual void RemoveClaim(Claim claim)
533  {
534  if (!TryRemoveClaim(claim))
535  {
536  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ClaimCannotBeRemoved", claim));
537  }
538  }
539 
540  [SecuritySafeCritical]
541  private void SafeAddClaims(IEnumerable<Claim> claims)
542  {
543  foreach (Claim claim in claims)
544  {
545  if (claim.Subject == this)
546  {
547  m_instanceClaims.Add(claim);
548  }
549  else
550  {
551  m_instanceClaims.Add(claim.Clone(this));
552  }
553  }
554  }
555 
556  [SecuritySafeCritical]
557  private void SafeAddClaim(Claim claim)
558  {
559  if (claim.Subject == this)
560  {
561  m_instanceClaims.Add(claim);
562  }
563  else
564  {
565  m_instanceClaims.Add(claim.Clone(this));
566  }
567  }
568 
574  public virtual IEnumerable<Claim> FindAll(Predicate<Claim> match)
575  {
576  if (match == null)
577  {
578  throw new ArgumentNullException("match");
579  }
580  List<Claim> list = new List<Claim>();
581  foreach (Claim claim in Claims)
582  {
583  if (match(claim))
584  {
585  list.Add(claim);
586  }
587  }
588  return list.AsReadOnly();
589  }
590 
596  public virtual IEnumerable<Claim> FindAll(string type)
597  {
598  if (type == null)
599  {
600  throw new ArgumentNullException("type");
601  }
602  List<Claim> list = new List<Claim>();
603  foreach (Claim claim in Claims)
604  {
605  if (claim != null && string.Equals(claim.Type, type, StringComparison.OrdinalIgnoreCase))
606  {
607  list.Add(claim);
608  }
609  }
610  return list.AsReadOnly();
611  }
612 
619  public virtual bool HasClaim(Predicate<Claim> match)
620  {
621  if (match == null)
622  {
623  throw new ArgumentNullException("match");
624  }
625  foreach (Claim claim in Claims)
626  {
627  if (match(claim))
628  {
629  return true;
630  }
631  }
632  return false;
633  }
634 
643  public virtual bool HasClaim(string type, string value)
644  {
645  if (type == null)
646  {
647  throw new ArgumentNullException("type");
648  }
649  if (value == null)
650  {
651  throw new ArgumentNullException("value");
652  }
653  foreach (Claim claim in Claims)
654  {
655  if (claim != null && claim != null && string.Equals(claim.Type, type, StringComparison.OrdinalIgnoreCase) && string.Equals(claim.Value, value, StringComparison.Ordinal))
656  {
657  return true;
658  }
659  }
660  return false;
661  }
662 
668  public virtual Claim FindFirst(Predicate<Claim> match)
669  {
670  if (match == null)
671  {
672  throw new ArgumentNullException("match");
673  }
674  foreach (Claim claim in Claims)
675  {
676  if (match(claim))
677  {
678  return claim;
679  }
680  }
681  return null;
682  }
683 
689  public virtual Claim FindFirst(string type)
690  {
691  if (type == null)
692  {
693  throw new ArgumentNullException("type");
694  }
695  foreach (Claim claim in Claims)
696  {
697  if (claim != null && string.Equals(claim.Type, type, StringComparison.OrdinalIgnoreCase))
698  {
699  return claim;
700  }
701  }
702  return null;
703  }
704 
705  [OnSerializing]
706  [SecurityCritical]
707  private void OnSerializingMethod(StreamingContext context)
708  {
709  if (!(this is ISerializable))
710  {
711  m_serializedClaims = SerializeClaims();
712  m_serializedNameType = m_nameType;
713  m_serializedRoleType = m_roleType;
714  }
715  }
716 
717  [OnDeserialized]
718  [SecurityCritical]
719  private void OnDeserializedMethod(StreamingContext context)
720  {
721  if (!(this is ISerializable))
722  {
723  if (!string.IsNullOrEmpty(m_serializedClaims))
724  {
725  DeserializeClaims(m_serializedClaims);
726  m_serializedClaims = null;
727  }
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);
730  }
731  }
732 
733  [OnDeserializing]
734  private void OnDeserializingMethod(StreamingContext context)
735  {
736  if (!(this is ISerializable))
737  {
738  m_instanceClaims = new List<Claim>();
739  m_externalClaims = new Collection<IEnumerable<Claim>>();
740  }
741  }
742 
748  [SecurityCritical]
749  [SecurityPermission(SecurityAction.Assert, SerializationFormatter = true)]
750  protected virtual void GetObjectData(SerializationInfo info, StreamingContext context)
751  {
752  if (info == null)
753  {
754  throw new ArgumentNullException("info");
755  }
756  BinaryFormatter binaryFormatter = new BinaryFormatter();
757  info.AddValue("System.Security.ClaimsIdentity.version", m_version);
758  if (!string.IsNullOrEmpty(m_authenticationType))
759  {
760  info.AddValue("System.Security.ClaimsIdentity.authenticationType", m_authenticationType);
761  }
762  info.AddValue("System.Security.ClaimsIdentity.nameClaimType", m_nameType);
763  info.AddValue("System.Security.ClaimsIdentity.roleClaimType", m_roleType);
764  if (!string.IsNullOrEmpty(m_label))
765  {
766  info.AddValue("System.Security.ClaimsIdentity.label", m_label);
767  }
768  if (m_actor != null)
769  {
770  using (MemoryStream memoryStream = new MemoryStream())
771  {
772  binaryFormatter.Serialize(memoryStream, m_actor, null, fCheck: false);
773  info.AddValue("System.Security.ClaimsIdentity.actor", Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length));
774  }
775  }
776  info.AddValue("System.Security.ClaimsIdentity.claims", SerializeClaims());
777  if (m_bootstrapContext != null)
778  {
779  using (MemoryStream memoryStream2 = new MemoryStream())
780  {
781  binaryFormatter.Serialize(memoryStream2, m_bootstrapContext, null, fCheck: false);
782  info.AddValue("System.Security.ClaimsIdentity.bootstrapContext", Convert.ToBase64String(memoryStream2.GetBuffer(), 0, (int)memoryStream2.Length));
783  }
784  }
785  }
786 
787  [SecurityCritical]
788  private void DeserializeClaims(string serializedClaims)
789  {
790  if (!string.IsNullOrEmpty(serializedClaims))
791  {
792  using (MemoryStream serializationStream = new MemoryStream(Convert.FromBase64String(serializedClaims)))
793  {
794  m_instanceClaims = (List<Claim>)new BinaryFormatter().Deserialize(serializationStream, null, fCheck: false);
795  for (int i = 0; i < m_instanceClaims.Count; i++)
796  {
797  m_instanceClaims[i].Subject = this;
798  }
799  }
800  }
801  if (m_instanceClaims == null)
802  {
803  m_instanceClaims = new List<Claim>();
804  }
805  }
806 
807  [SecurityCritical]
808  private string SerializeClaims()
809  {
810  using (MemoryStream memoryStream = new MemoryStream())
811  {
812  new BinaryFormatter().Serialize(memoryStream, m_instanceClaims, null, fCheck: false);
813  return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
814  }
815  }
816 
817  private bool IsCircular(ClaimsIdentity subject)
818  {
819  if (this == subject)
820  {
821  return true;
822  }
823  ClaimsIdentity claimsIdentity = subject;
824  while (claimsIdentity.Actor != null)
825  {
826  if (this == claimsIdentity.Actor)
827  {
828  return true;
829  }
830  claimsIdentity = claimsIdentity.Actor;
831  }
832  return false;
833  }
834 
835  private void Initialize(BinaryReader reader)
836  {
837  if (reader == null)
838  {
839  throw new ArgumentNullException("reader");
840  }
841  SerializationMask serializationMask = (SerializationMask)reader.ReadInt32();
842  if ((serializationMask & SerializationMask.AuthenticationType) == SerializationMask.AuthenticationType)
843  {
844  m_authenticationType = reader.ReadString();
845  }
846  if ((serializationMask & SerializationMask.BootstrapConext) == SerializationMask.BootstrapConext)
847  {
848  m_bootstrapContext = reader.ReadString();
849  }
850  if ((serializationMask & SerializationMask.NameClaimType) == SerializationMask.NameClaimType)
851  {
852  m_nameType = reader.ReadString();
853  }
854  else
855  {
856  m_nameType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
857  }
858  if ((serializationMask & SerializationMask.RoleClaimType) == SerializationMask.RoleClaimType)
859  {
860  m_roleType = reader.ReadString();
861  }
862  else
863  {
864  m_roleType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
865  }
866  if ((serializationMask & SerializationMask.HasClaims) == SerializationMask.HasClaims)
867  {
868  int num = reader.ReadInt32();
869  for (int i = 0; i < num; i++)
870  {
871  Claim item = new Claim(reader, this);
872  m_instanceClaims.Add(item);
873  }
874  }
875  }
876 
879  protected virtual Claim CreateClaim(BinaryReader reader)
880  {
881  if (reader == null)
882  {
883  throw new ArgumentNullException("reader");
884  }
885  return new Claim(reader, this);
886  }
887 
890  public virtual void WriteTo(BinaryWriter writer)
891  {
892  WriteTo(writer, null);
893  }
894 
898  protected virtual void WriteTo(BinaryWriter writer, byte[] userData)
899  {
900  if (writer == null)
901  {
902  throw new ArgumentNullException("writer");
903  }
904  int num = 0;
905  SerializationMask serializationMask = SerializationMask.None;
906  if (m_authenticationType != null)
907  {
908  serializationMask |= SerializationMask.AuthenticationType;
909  num++;
910  }
911  if (m_bootstrapContext != null)
912  {
913  string text = m_bootstrapContext as string;
914  if (text != null)
915  {
916  serializationMask |= SerializationMask.BootstrapConext;
917  num++;
918  }
919  }
920  if (!string.Equals(m_nameType, "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", StringComparison.Ordinal))
921  {
922  serializationMask |= SerializationMask.NameClaimType;
923  num++;
924  }
925  if (!string.Equals(m_roleType, "http://schemas.microsoft.com/ws/2008/06/identity/claims/role", StringComparison.Ordinal))
926  {
927  serializationMask |= SerializationMask.RoleClaimType;
928  num++;
929  }
930  if (!string.IsNullOrWhiteSpace(m_label))
931  {
932  serializationMask |= SerializationMask.HasLabel;
933  num++;
934  }
935  if (m_instanceClaims.Count > 0)
936  {
937  serializationMask |= SerializationMask.HasClaims;
938  num++;
939  }
940  if (m_actor != null)
941  {
942  serializationMask |= SerializationMask.Actor;
943  num++;
944  }
945  if (userData != null && userData.Length != 0)
946  {
947  num++;
948  serializationMask |= SerializationMask.UserData;
949  }
950  writer.Write((int)serializationMask);
951  writer.Write(num);
952  if ((serializationMask & SerializationMask.AuthenticationType) == SerializationMask.AuthenticationType)
953  {
954  writer.Write(m_authenticationType);
955  }
956  if ((serializationMask & SerializationMask.BootstrapConext) == SerializationMask.BootstrapConext)
957  {
958  writer.Write(m_bootstrapContext as string);
959  }
960  if ((serializationMask & SerializationMask.NameClaimType) == SerializationMask.NameClaimType)
961  {
962  writer.Write(m_nameType);
963  }
964  if ((serializationMask & SerializationMask.RoleClaimType) == SerializationMask.RoleClaimType)
965  {
966  writer.Write(m_roleType);
967  }
968  if ((serializationMask & SerializationMask.HasLabel) == SerializationMask.HasLabel)
969  {
970  writer.Write(m_label);
971  }
972  if ((serializationMask & SerializationMask.HasClaims) == SerializationMask.HasClaims)
973  {
974  writer.Write(m_instanceClaims.Count);
975  foreach (Claim instanceClaim in m_instanceClaims)
976  {
977  instanceClaim.WriteTo(writer);
978  }
979  }
980  if ((serializationMask & SerializationMask.Actor) == SerializationMask.Actor)
981  {
982  m_actor.WriteTo(writer);
983  }
984  if ((serializationMask & SerializationMask.UserData) == SerializationMask.UserData)
985  {
986  writer.Write(userData.Length);
987  writer.Write(userData);
988  }
989  writer.Flush();
990  }
991 
992  [SecurityCritical]
993  [SecurityPermission(SecurityAction.Assert, SerializationFormatter = true)]
994  private void Deserialize(SerializationInfo info, StreamingContext context, bool useContext)
995  {
996  if (info == null)
997  {
998  throw new ArgumentNullException("info");
999  }
1000  BinaryFormatter binaryFormatter = (!useContext) ? new BinaryFormatter() : new BinaryFormatter(null, context);
1001  SerializationInfoEnumerator enumerator = info.GetEnumerator();
1002  while (enumerator.MoveNext())
1003  {
1004  switch (enumerator.Name)
1005  {
1006  case "System.Security.ClaimsIdentity.version":
1007  {
1008  string @string = info.GetString("System.Security.ClaimsIdentity.version");
1009  break;
1010  }
1011  case "System.Security.ClaimsIdentity.authenticationType":
1012  m_authenticationType = info.GetString("System.Security.ClaimsIdentity.authenticationType");
1013  break;
1014  case "System.Security.ClaimsIdentity.nameClaimType":
1015  m_nameType = info.GetString("System.Security.ClaimsIdentity.nameClaimType");
1016  break;
1017  case "System.Security.ClaimsIdentity.roleClaimType":
1018  m_roleType = info.GetString("System.Security.ClaimsIdentity.roleClaimType");
1019  break;
1020  case "System.Security.ClaimsIdentity.label":
1021  m_label = info.GetString("System.Security.ClaimsIdentity.label");
1022  break;
1023  case "System.Security.ClaimsIdentity.actor":
1024  using (MemoryStream serializationStream2 = new MemoryStream(Convert.FromBase64String(info.GetString("System.Security.ClaimsIdentity.actor"))))
1025  {
1026  m_actor = (ClaimsIdentity)binaryFormatter.Deserialize(serializationStream2, null, fCheck: false);
1027  }
1028  break;
1029  case "System.Security.ClaimsIdentity.claims":
1030  DeserializeClaims(info.GetString("System.Security.ClaimsIdentity.claims"));
1031  break;
1032  case "System.Security.ClaimsIdentity.bootstrapContext":
1033  using (MemoryStream serializationStream = new MemoryStream(Convert.FromBase64String(info.GetString("System.Security.ClaimsIdentity.bootstrapContext"))))
1034  {
1035  m_bootstrapContext = binaryFormatter.Deserialize(serializationStream, null, fCheck: false);
1036  }
1037  break;
1038  }
1039  }
1040  }
1041  }
1042 }
Converts a base data type to another base data type.
Definition: Convert.cs:10
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.
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.
Definition: List.cs:296
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.
Definition: Claim.cs:94
virtual void WriteTo(BinaryWriter writer)
Definition: Claim.cs:360
void RemoveAt(int index)
Removes the element at the specified index of the T:System.Collections.Generic.List`1.
Definition: List.cs:1375
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...
void Serialize(Stream serializationStream, object graph)
Serializes the object, or graph of objects with the specified top (root), to the given stream.
Definition: __Canon.cs:3
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-...
object Deserialize(Stream serializationStream)
Deserializes the specified stream into an object graph.
Serializes and deserializes an object, or an entire graph of connected objects, in binary format.
static string ToBase64String(byte[] inArray)
Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded ...
Definition: Convert.cs:4413
virtual bool TryRemoveClaim(Claim claim)
Attempts to remove a claim from the claims identity.
string Value
Gets the value of the claim.
Definition: Claim.cs:98
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 [] 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.
Definition: List.cs:510
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....
Definition: Environment.cs:21
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...
Definition: MemoryStream.cs:13
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.
Definition: BinaryReader.cs:10
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 claim.
Definition: Claim.cs:10
Represents a claims-based identity.
int Count
Gets the number of elements actually contained in the T:System.Collections.ObjectModel....
Definition: Collection.cs:26
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.
Definition: Claim.cs:81
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.
Definition: ISerializable.cs:8
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.
Definition: IIdentity.cs:23
static unsafe byte [] FromBase64String(string s)
Converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit un...
Definition: Convert.cs:4692
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.
Definition: IIdentity.cs:8
virtual Claim Clone()
Returns a new T:System.Security.Claims.Claim object copied from this object. The new claim does not h...
Definition: Claim.cs:270
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.
Definition: IIdentity.cs:14
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.
Definition: Collection.cs:15
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.
Definition: BinaryWriter.cs:12
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.