5 // Miguel de Icaza (miguel@xamarin.com)
7 // Copyright 2014 Xamarin Inc
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
31 using System.Security.Principal;
32 using System.Runtime.Serialization;
33 using System.Threading;
35 namespace System.Security.Claims {
37 [SerializableAttribute]
38 public class ClaimsPrincipal : IPrincipal
40 List<ClaimsIdentity> identities;
42 static ClaimsPrincipal ()
44 ClaimsPrincipalSelector = DefaultClaimsPrincipal;
47 static ClaimsPrincipal DefaultClaimsPrincipal ()
49 return Thread.CurrentPrincipal as ClaimsPrincipal;
52 public ClaimsPrincipal ()
54 identities = new List<ClaimsIdentity>();
57 public ClaimsPrincipal (IEnumerable<ClaimsIdentity> identities)
59 if (identities == null)
60 throw new ArgumentNullException ("identities");
62 this.identities = new List<ClaimsIdentity> (identities);
65 public ClaimsPrincipal (IIdentity identity)
68 throw new ArgumentNullException ("identity");
70 identities = new List<ClaimsIdentity> ();
71 identities.Add (new ClaimsIdentity (identity));
74 public ClaimsPrincipal (IPrincipal principal)
76 if (principal == null)
77 throw new ArgumentNullException ("principal");
78 var cp = principal as ClaimsPrincipal;
80 identities = new List<ClaimsIdentity> (cp.identities);
82 identities = new List<ClaimsIdentity> ();
83 identities.Add (new ClaimsIdentity (principal.Identity));
88 protected ClaimsPrincipal (SerializationInfo info, StreamingContext context)
90 throw new NotImplementedException ();
93 public virtual IEnumerable<Claim> Claims {
95 foreach (var ci in identities)
96 foreach (var claim in ci.Claims)
101 public static Func<ClaimsPrincipal> ClaimsPrincipalSelector { get; set; }
103 public static ClaimsPrincipal Current {
105 return ClaimsPrincipalSelector ();
109 public virtual IEnumerable<ClaimsIdentity> Identities {
115 public static Func<IEnumerable<ClaimsIdentity>, ClaimsIdentity> PrimaryIdentitySelector { get; set; }
117 public virtual IIdentity Identity {
119 if (identities == null)
120 throw new ArgumentNullException ("Identities");
122 if (PrimaryIdentitySelector != null)
123 return PrimaryIdentitySelector (identities);
125 ClaimsIdentity firstCI = null;
126 foreach (var ident in identities){
127 if (ident is WindowsIdentity)
129 if (firstCI == null && ident is ClaimsIdentity)
130 firstCI = ident as ClaimsIdentity;
136 public virtual void AddIdentities (IEnumerable<ClaimsIdentity> identities)
138 if (identities == null)
139 throw new ArgumentNullException ("identities");
140 foreach (var id in identities)
141 this.identities.Add (id);
144 public virtual void AddIdentity (ClaimsIdentity identity)
146 if (identity == null)
147 throw new ArgumentNullException ("identity");
148 identities.Add (identity);
151 public virtual IEnumerable<Claim> FindAll (Predicate<Claim> match)
154 throw new ArgumentNullException ("match");
155 foreach (var claim in Claims){
161 public virtual Claim FindFirst (Predicate<Claim> match)
164 throw new ArgumentNullException ("match");
165 foreach (var claim in Claims)
171 public virtual bool HasClaim (Predicate<Claim> match)
174 throw new ArgumentNullException ("match");
175 foreach (var claim in Claims)
181 public virtual bool IsInRole (string role)
183 foreach (var id in identities){
184 if (id.HasClaim (id.RoleClaimType, role))