mscorlib(4.0.0.0) API with additions
Claim.cs
3 using System.IO;
5 
7 {
9  [Serializable]
10  public class Claim
11  {
12  private enum SerializationMask
13  {
14  None = 0,
15  NameClaimType = 1,
16  RoleClaimType = 2,
17  StringType = 4,
18  Issuer = 8,
19  OriginalIssuerEqualsIssuer = 0x10,
20  OriginalIssuer = 0x20,
21  HasProperties = 0x40,
22  UserData = 0x80
23  }
24 
25  private string m_issuer;
26 
27  private string m_originalIssuer;
28 
29  private string m_type;
30 
31  private string m_value;
32 
33  private string m_valueType;
34 
35  [NonSerialized]
36  private byte[] m_userSerializationData;
37 
38  private Dictionary<string, string> m_properties;
39 
40  [NonSerialized]
41  private object m_propertyLock = new object();
42 
43  [NonSerialized]
44  private ClaimsIdentity m_subject;
45 
48  protected virtual byte[] CustomSerializationData => m_userSerializationData;
49 
52  public string Issuer => m_issuer;
53 
56  public string OriginalIssuer => m_originalIssuer;
57 
61  {
62  get
63  {
64  if (m_properties == null)
65  {
66  lock (m_propertyLock)
67  {
68  if (m_properties == null)
69  {
70  m_properties = new Dictionary<string, string>();
71  }
72  }
73  }
74  return m_properties;
75  }
76  }
77 
80  public ClaimsIdentity Subject
81  {
82  get
83  {
84  return m_subject;
85  }
86  internal set
87  {
88  m_subject = value;
89  }
90  }
91 
94  public string Type => m_type;
95 
98  public string Value => m_value;
99 
102  public string ValueType => m_valueType;
103 
106  public Claim(BinaryReader reader)
107  : this(reader, null)
108  {
109  }
110 
114  public Claim(BinaryReader reader, ClaimsIdentity subject)
115  {
116  if (reader == null)
117  {
118  throw new ArgumentNullException("reader");
119  }
120  Initialize(reader, subject);
121  }
122 
128  public Claim(string type, string value)
129  : this(type, value, "http://www.w3.org/2001/XMLSchema#string", "LOCAL AUTHORITY", "LOCAL AUTHORITY", null)
130  {
131  }
132 
139  public Claim(string type, string value, string valueType)
140  : this(type, value, valueType, "LOCAL AUTHORITY", "LOCAL AUTHORITY", null)
141  {
142  }
143 
151  public Claim(string type, string value, string valueType, string issuer)
152  : this(type, value, valueType, issuer, issuer, null)
153  {
154  }
155 
164  public Claim(string type, string value, string valueType, string issuer, string originalIssuer)
165  : this(type, value, valueType, issuer, originalIssuer, null)
166  {
167  }
168 
178  public Claim(string type, string value, string valueType, string issuer, string originalIssuer, ClaimsIdentity subject)
179  : this(type, value, valueType, issuer, originalIssuer, subject, null, null)
180  {
181  }
182 
183  internal Claim(string type, string value, string valueType, string issuer, string originalIssuer, ClaimsIdentity subject, string propertyKey, string propertyValue)
184  {
185  if (type == null)
186  {
187  throw new ArgumentNullException("type");
188  }
189  if (value == null)
190  {
191  throw new ArgumentNullException("value");
192  }
193  m_type = type;
194  m_value = value;
195  if (string.IsNullOrEmpty(valueType))
196  {
197  m_valueType = "http://www.w3.org/2001/XMLSchema#string";
198  }
199  else
200  {
201  m_valueType = valueType;
202  }
203  if (string.IsNullOrEmpty(issuer))
204  {
205  m_issuer = "LOCAL AUTHORITY";
206  }
207  else
208  {
209  m_issuer = issuer;
210  }
211  if (string.IsNullOrEmpty(originalIssuer))
212  {
213  m_originalIssuer = m_issuer;
214  }
215  else
216  {
217  m_originalIssuer = originalIssuer;
218  }
219  m_subject = subject;
220  if (propertyKey != null)
221  {
222  Properties.Add(propertyKey, propertyValue);
223  }
224  }
225 
228  protected Claim(Claim other)
229  : this(other, other?.m_subject)
230  {
231  }
232 
236  protected Claim(Claim other, ClaimsIdentity subject)
237  {
238  if (other == null)
239  {
240  throw new ArgumentNullException("other");
241  }
242  m_issuer = other.m_issuer;
243  m_originalIssuer = other.m_originalIssuer;
244  m_subject = subject;
245  m_type = other.m_type;
246  m_value = other.m_value;
247  m_valueType = other.m_valueType;
248  if (other.m_properties != null)
249  {
250  m_properties = new Dictionary<string, string>();
251  foreach (string key in other.m_properties.Keys)
252  {
253  m_properties.Add(key, other.m_properties[key]);
254  }
255  }
256  if (other.m_userSerializationData != null)
257  {
258  m_userSerializationData = (other.m_userSerializationData.Clone() as byte[]);
259  }
260  }
261 
262  [OnDeserialized]
263  private void OnDeserializedMethod(StreamingContext context)
264  {
265  m_propertyLock = new object();
266  }
267 
270  public virtual Claim Clone()
271  {
272  return Clone(null);
273  }
274 
278  public virtual Claim Clone(ClaimsIdentity identity)
279  {
280  return new Claim(this, identity);
281  }
282 
283  private void Initialize(BinaryReader reader, ClaimsIdentity subject)
284  {
285  if (reader == null)
286  {
287  throw new ArgumentNullException("reader");
288  }
289  m_subject = subject;
290  SerializationMask serializationMask = (SerializationMask)reader.ReadInt32();
291  int num = 1;
292  int num2 = reader.ReadInt32();
293  m_value = reader.ReadString();
294  if ((serializationMask & SerializationMask.NameClaimType) == SerializationMask.NameClaimType)
295  {
296  m_type = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
297  }
298  else if ((serializationMask & SerializationMask.RoleClaimType) == SerializationMask.RoleClaimType)
299  {
300  m_type = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
301  }
302  else
303  {
304  m_type = reader.ReadString();
305  num++;
306  }
307  if ((serializationMask & SerializationMask.StringType) == SerializationMask.StringType)
308  {
309  m_valueType = reader.ReadString();
310  num++;
311  }
312  else
313  {
314  m_valueType = "http://www.w3.org/2001/XMLSchema#string";
315  }
316  if ((serializationMask & SerializationMask.Issuer) == SerializationMask.Issuer)
317  {
318  m_issuer = reader.ReadString();
319  num++;
320  }
321  else
322  {
323  m_issuer = "LOCAL AUTHORITY";
324  }
325  if ((serializationMask & SerializationMask.OriginalIssuerEqualsIssuer) == SerializationMask.OriginalIssuerEqualsIssuer)
326  {
327  m_originalIssuer = m_issuer;
328  }
329  else if ((serializationMask & SerializationMask.OriginalIssuer) == SerializationMask.OriginalIssuer)
330  {
331  m_originalIssuer = reader.ReadString();
332  num++;
333  }
334  else
335  {
336  m_originalIssuer = "LOCAL AUTHORITY";
337  }
338  if ((serializationMask & SerializationMask.HasProperties) == SerializationMask.HasProperties)
339  {
340  int num3 = reader.ReadInt32();
341  for (int i = 0; i < num3; i++)
342  {
343  Properties.Add(reader.ReadString(), reader.ReadString());
344  }
345  }
346  if ((serializationMask & SerializationMask.UserData) == SerializationMask.UserData)
347  {
348  int count = reader.ReadInt32();
349  m_userSerializationData = reader.ReadBytes(count);
350  num++;
351  }
352  for (int j = num; j < num2; j++)
353  {
354  reader.ReadString();
355  }
356  }
357 
360  public virtual void WriteTo(BinaryWriter writer)
361  {
362  WriteTo(writer, null);
363  }
364 
368  protected virtual void WriteTo(BinaryWriter writer, byte[] userData)
369  {
370  if (writer == null)
371  {
372  throw new ArgumentNullException("writer");
373  }
374  int num = 1;
375  SerializationMask serializationMask = SerializationMask.None;
376  if (string.Equals(m_type, "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"))
377  {
378  serializationMask |= SerializationMask.NameClaimType;
379  }
380  else if (string.Equals(m_type, "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"))
381  {
382  serializationMask |= SerializationMask.RoleClaimType;
383  }
384  else
385  {
386  num++;
387  }
388  if (!string.Equals(m_valueType, "http://www.w3.org/2001/XMLSchema#string", StringComparison.Ordinal))
389  {
390  num++;
391  serializationMask |= SerializationMask.StringType;
392  }
393  if (!string.Equals(m_issuer, "LOCAL AUTHORITY", StringComparison.Ordinal))
394  {
395  num++;
396  serializationMask |= SerializationMask.Issuer;
397  }
398  if (string.Equals(m_originalIssuer, m_issuer, StringComparison.Ordinal))
399  {
400  serializationMask |= SerializationMask.OriginalIssuerEqualsIssuer;
401  }
402  else if (!string.Equals(m_originalIssuer, "LOCAL AUTHORITY", StringComparison.Ordinal))
403  {
404  num++;
405  serializationMask |= SerializationMask.OriginalIssuer;
406  }
407  if (Properties.Count > 0)
408  {
409  num++;
410  serializationMask |= SerializationMask.HasProperties;
411  }
412  if (userData != null && userData.Length != 0)
413  {
414  num++;
415  serializationMask |= SerializationMask.UserData;
416  }
417  writer.Write((int)serializationMask);
418  writer.Write(num);
419  writer.Write(m_value);
420  if ((serializationMask & SerializationMask.NameClaimType) != SerializationMask.NameClaimType && (serializationMask & SerializationMask.RoleClaimType) != SerializationMask.RoleClaimType)
421  {
422  writer.Write(m_type);
423  }
424  if ((serializationMask & SerializationMask.StringType) == SerializationMask.StringType)
425  {
426  writer.Write(m_valueType);
427  }
428  if ((serializationMask & SerializationMask.Issuer) == SerializationMask.Issuer)
429  {
430  writer.Write(m_issuer);
431  }
432  if ((serializationMask & SerializationMask.OriginalIssuer) == SerializationMask.OriginalIssuer)
433  {
434  writer.Write(m_originalIssuer);
435  }
436  if ((serializationMask & SerializationMask.HasProperties) == SerializationMask.HasProperties)
437  {
438  writer.Write(Properties.Count);
439  foreach (string key in Properties.Keys)
440  {
441  writer.Write(key);
442  writer.Write(Properties[key]);
443  }
444  }
445  if ((serializationMask & SerializationMask.UserData) == SerializationMask.UserData)
446  {
447  writer.Write(userData.Length);
448  writer.Write(userData);
449  }
450  writer.Flush();
451  }
452 
455  public override string ToString()
456  {
457  return string.Format(CultureInfo.InvariantCulture, "{0}: {1}", m_type, m_value);
458  }
459  }
460 }
Claim(Claim other, ClaimsIdentity subject)
Initializes a new instance of the T:System.Security.Claims.Claim class with the specified security cl...
Definition: Claim.cs:236
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
string OriginalIssuer
Gets the original issuer of the claim.
Definition: Claim.cs:56
Claim(string type, string value, string valueType, string issuer)
Initializes a new instance of the T:System.Security.Claims.Claim class with the specified claim type,...
Definition: Claim.cs:151
virtual void WriteTo(BinaryWriter writer, byte[] userData)
Writes this T:System.Security.Claims.Claim to the writer.
Definition: Claim.cs:368
virtual int ReadInt32()
Reads a 4-byte signed integer from the current stream and advances the current position of the stream...
virtual void WriteTo(BinaryWriter writer)
Definition: Claim.cs:360
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...
Claim(string type, string value)
Initializes a new instance of the T:System.Security.Claims.Claim class with the specified claim type,...
Definition: Claim.cs:128
ICollection< TKey > Keys
Gets an T:System.Collections.Generic.ICollection`1 containing the keys of the T:System....
Definition: IDictionary.cs:29
Definition: __Canon.cs:3
virtual Claim Clone(ClaimsIdentity identity)
Returns a new T:System.Security.Claims.Claim object copied from this object. The subject of the new c...
Definition: Claim.cs:278
Describes the source and destination of a given serialized stream, and provides an additional caller-...
string Value
Gets the value of the claim.
Definition: Claim.cs:98
IDictionary< string, string > Properties
Gets a dictionary that contains additional properties associated with this claim.
Definition: Claim.cs:61
Claim(string type, string value, string valueType, string issuer, string originalIssuer)
Initializes a new instance of the T:System.Security.Claims.Claim class with the specified claim type,...
Definition: Claim.cs:164
KeyCollection Keys
Gets a collection containing the keys in the T:System.Collections.Generic.Dictionary`2.
Definition: Dictionary.cs:902
virtual byte [] ReadBytes(int count)
Reads the specified number of bytes from the current stream into a byte array and advances the curren...
Claim(string type, string value, string valueType)
Initializes a new instance of the T:System.Security.Claims.Claim class with the specified claim type,...
Definition: Claim.cs:139
Reads primitive data types as binary values in a specific encoding.
Definition: BinaryReader.cs:10
Claim(BinaryReader reader, ClaimsIdentity subject)
Initializes a new instance of the T:System.Security.Claims.Claim class with the specified reader,...
Definition: Claim.cs:114
Represents a claim.
Definition: Claim.cs:10
Claim(BinaryReader reader)
Definition: Claim.cs:106
Represents a claims-based identity.
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
virtual void Write(bool value)
Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing t...
override string ToString()
Returns a string representation of this T:System.Security.Claims.Claim object.
Definition: Claim.cs:455
ClaimsIdentity Subject
Gets the subject of the claim.
Definition: Claim.cs:81
string Issuer
Gets the issuer of the claim.
Definition: Claim.cs:52
void Add(TKey key, TValue value)
Adds an element with the provided key and value to the T:System.Collections.Generic....
virtual byte [] CustomSerializationData
Definition: Claim.cs:48
Claim(string type, string value, string valueType, string issuer, string originalIssuer, ClaimsIdentity subject)
Initializes a new instance of the T:System.Security.Claims.Claim class with the specified claim type,...
Definition: Claim.cs:178
void Add(TKey key, TValue value)
Adds the specified key and value to the dictionary.
Definition: Dictionary.cs:1244
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
virtual string ReadString()
Reads a string from the current stream. The string is prefixed with the length, encoded as an integer...
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Writes primitive types in binary to a stream and supports writing strings in a specific encoding.
Definition: BinaryWriter.cs:12
Provides the base class for value types.
Definition: ValueType.cs:12