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 namespace System.Security.Claims {
36 public class ClaimsIdentity : IIdentity {
37 [NonSerializedAttribute]
38 public const string DefaultNameClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
39 [NonSerializedAttribute]
40 public const string DefaultRoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
41 [NonSerializedAttribute]
42 public const string DefaultIssuer = "LOCAL AUTHORITY";
47 public ClaimsIdentity ()
48 : this (claims: null, authenticationType: null, nameType: null, roleType: null)
51 public ClaimsIdentity (string authenticationType)
52 : this (claims: null, authenticationType: authenticationType, nameType: null, roleType: null)
55 public ClaimsIdentity (IEnumerable<Claim> claims, string authenticationType)
56 : this (claims, authenticationType, null, null)
59 public ClaimsIdentity (string authenticationType, string nameType, string roleType)
60 : this (claims: null, authenticationType: authenticationType, nameType: nameType, roleType: roleType)
63 public ClaimsIdentity (IIdentity identity) : this (identity: identity, claims: null)
67 public ClaimsIdentity(IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType)
68 : this (identity: null, claims: claims, authenticationType: authenticationType, nameType: nameType, roleType: roleType)
70 claims = claims == null ? new List<Claim> (): new List<Claim> (claims);
72 AuthenticationType = authenticationType;
74 // Special case: if empty, set to null.
75 if (authenticationType == "")
76 AuthenticationType = null;
78 NameClaimType = nameType == null ? DefaultNameClaimType : nameType;
79 RoleClaimType = roleType == null ? DefaultRoleClaimType : roleType;
82 public ClaimsIdentity (IIdentity identity, IEnumerable<Claim> claims)
83 : this (identity, claims, authenticationType: null, nameType: null, roleType: null)
86 public ClaimsIdentity (IIdentity identity, IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType)
88 var ci = identity as ClaimsIdentity;
89 NameClaimType = nameType == null ? DefaultNameClaimType : nameType;
90 RoleClaimType = roleType == null ? DefaultRoleClaimType : roleType;
92 this.claims = new List<Claim> ();
95 BootstrapContext = ci.BootstrapContext;
96 foreach (var c in ci.Claims)
99 foreach (var c in claims)
102 NameClaimType = ci.NameClaimType;
103 RoleClaimType = ci.RoleClaimType;
104 AuthenticationType = ci.AuthenticationType;
109 protected ClaimsIdentity (SerializationInfo info)
111 throw new NotImplementedException ();
115 protected ClaimsIdentity (SerializationInfo info, StreamingContext context)
118 throw new ArgumentNullException ("info");
119 throw new NotImplementedException ();
122 public ClaimsIdentity Actor {
128 throw new InvalidOperationException ("can not set the Actor property to this instance");
133 public virtual string AuthenticationType { get; private set; }
134 public object BootstrapContext { get; set; }
135 public string Label { get; set; }
136 public virtual string Name {
138 var target = NameClaimType;
139 foreach (var c in claims){
140 if (c.Type == target)
146 public string NameClaimType { get; private set; }
147 public string RoleClaimType { get; private set; }
149 public virtual IEnumerable<Claim> Claims {
155 public virtual bool IsAuthenticated {
157 return AuthenticationType != null && AuthenticationType != "";
161 public virtual void AddClaim (Claim claim)
164 throw new ArgumentNullException ("claim");
168 public virtual void AddClaims (IEnumerable<Claim> claims)
171 throw new ArgumentNullException ("claims");
172 foreach (var c in claims)
176 public virtual ClaimsIdentity Clone ()
178 return new ClaimsIdentity (null, claims, AuthenticationType, NameClaimType, RoleClaimType){
179 BootstrapContext = this.BootstrapContext,
185 public virtual IEnumerable<Claim> FindAll(Predicate<Claim> match)
188 throw new ArgumentNullException ("match");
189 foreach (var c in claims)
194 public virtual IEnumerable<Claim> FindAll(string type)
197 throw new ArgumentNullException ("type");
198 foreach (var c in claims)
203 public virtual Claim FindFirst (Predicate<Claim> match)
206 throw new ArgumentNullException ("match");
207 foreach (var c in claims)
213 public virtual Claim FindFirst (string type)
216 throw new ArgumentNullException ("type");
217 foreach (var c in claims)
223 public virtual bool HasClaim (Predicate<Claim> match)
226 throw new ArgumentNullException ("match");
227 foreach (var c in claims)
233 public virtual bool HasClaim (string type, string value)
236 throw new ArgumentNullException ("type");
238 throw new ArgumentNullException ("value");
239 foreach (var c in claims){
240 if (c.Type == type && c.Value == value)
246 public virtual void RemoveClaim (Claim claim)
248 if (!TryRemoveClaim (claim))
249 throw new InvalidOperationException ();
252 [MonoTODO ("This one should return false if the claim is owned by someone else, this does not exist yet")]
253 public virtual bool TryRemoveClaim (Claim claim)
257 claims.Remove (claim);