mscorlib(4.0.0.0) API with additions
GenericPrincipal.cs
4 
6 {
9  [ComVisible(true)]
11  {
12  private IIdentity m_identity;
13 
14  private string[] m_roles;
15 
18  public override IIdentity Identity => m_identity;
19 
24  public GenericPrincipal(IIdentity identity, string[] roles)
25  {
26  if (identity == null)
27  {
28  throw new ArgumentNullException("identity");
29  }
30  m_identity = identity;
31  if (roles != null)
32  {
33  m_roles = new string[roles.Length];
34  for (int i = 0; i < roles.Length; i++)
35  {
36  m_roles[i] = roles[i];
37  }
38  }
39  else
40  {
41  m_roles = null;
42  }
43  AddIdentityWithRoles(m_identity, m_roles);
44  }
45 
46  [OnDeserialized]
47  private void OnDeserializedMethod(StreamingContext context)
48  {
49  ClaimsIdentity claimsIdentity = null;
50  foreach (ClaimsIdentity identity in base.Identities)
51  {
52  if (identity != null)
53  {
54  claimsIdentity = identity;
55  break;
56  }
57  }
58  if (m_roles != null && m_roles.Length != 0 && claimsIdentity != null)
59  {
60  claimsIdentity.ExternalClaims.Add(new RoleClaimProvider("LOCAL AUTHORITY", m_roles, claimsIdentity).Claims);
61  }
62  else if (claimsIdentity == null)
63  {
64  AddIdentityWithRoles(m_identity, m_roles);
65  }
66  }
67 
68  [SecuritySafeCritical]
69  private void AddIdentityWithRoles(IIdentity identity, string[] roles)
70  {
71  ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;
72  claimsIdentity = ((claimsIdentity == null) ? new ClaimsIdentity(identity) : claimsIdentity.Clone());
73  if (roles != null && roles.Length != 0)
74  {
75  claimsIdentity.ExternalClaims.Add(new RoleClaimProvider("LOCAL AUTHORITY", roles, claimsIdentity).Claims);
76  }
77  base.AddIdentity(claimsIdentity);
78  }
79 
84  public override bool IsInRole(string role)
85  {
86  if (role == null || m_roles == null)
87  {
88  return false;
89  }
90  for (int i = 0; i < m_roles.Length; i++)
91  {
92  if (m_roles[i] != null && string.Compare(m_roles[i], role, StringComparison.OrdinalIgnoreCase) == 0)
93  {
94  return true;
95  }
96  }
97  return base.IsInRole(role);
98  }
99  }
100 }
override IIdentity Identity
Gets the T:System.Security.Principal.GenericIdentity of the user represented by the current T:System....
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
Definition: __Canon.cs:3
Describes the source and destination of a given serialized stream, and provides an additional caller-...
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...
virtual ClaimsIdentity Clone()
Returns a new T:System.Security.Claims.ClaimsIdentity copied from this claims identity.
Represents a claims-based identity.
GenericPrincipal(IIdentity identity, string[] roles)
Initializes a new instance of the T:System.Security.Principal.GenericPrincipal class from a user iden...
override bool IsInRole(string role)
Determines whether the current T:System.Security.Principal.GenericPrincipal belongs to the specified ...
Specifies that the class can be serialized.
Defines the basic functionality of an identity object.
Definition: IIdentity.cs:8
Represents a generic principal.