//
// Authors:
// Miguel de Icaza (miguel@xamarin.com)
+// Marek Safar (marek.safar@gmail.com)
//
// Copyright 2014 Xamarin Inc
//
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_4_5
-using System;
+
using System.Collections.Generic;
using System.Security.Principal;
using System.Runtime.Serialization;
+
namespace System.Security.Claims {
[Serializable]
[NonSerializedAttribute]
public const string DefaultIssuer = "LOCAL AUTHORITY";
- List<Claim> claims;
+ readonly List<Claim> claims;
ClaimsIdentity actor;
+ readonly string auth_type;
public ClaimsIdentity ()
: this (claims: null, authenticationType: null, nameType: null, roleType: null)
{ }
+ public ClaimsIdentity(IEnumerable<Claim> claims)
+ : this (claims: claims, authenticationType: null, nameType: null, roleType: null)
+ { }
+
public ClaimsIdentity (string authenticationType)
: this (claims: null, authenticationType: authenticationType, nameType: null, roleType: null)
{ }
public ClaimsIdentity(IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType)
: this (identity: null, claims: claims, authenticationType: authenticationType, nameType: nameType, roleType: roleType)
{
- claims = claims == null ? new List<Claim> (): new List<Claim> (claims);
-
- AuthenticationType = authenticationType;
-
- // Special case: if empty, set to null.
- if (authenticationType == "")
- AuthenticationType = null;
-
- NameClaimType = nameType == null ? DefaultNameClaimType : nameType;
- RoleClaimType = roleType == null ? DefaultRoleClaimType : roleType;
}
public ClaimsIdentity (IIdentity identity, IEnumerable<Claim> claims)
: this (identity, claims, authenticationType: null, nameType: null, roleType: null)
- { }
+ {
+ }
public ClaimsIdentity (IIdentity identity, IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType)
{
- var ci = identity as ClaimsIdentity;
- NameClaimType = nameType == null ? DefaultNameClaimType : nameType;
- RoleClaimType = roleType == null ? DefaultRoleClaimType : roleType;
-
+ NameClaimType = string.IsNullOrEmpty (nameType) ? DefaultNameClaimType : nameType;
+ RoleClaimType = string.IsNullOrEmpty (roleType) ? DefaultRoleClaimType : roleType;
+ auth_type = authenticationType;
+
this.claims = new List<Claim> ();
- if (ci != null){
- actor = ci.Actor;
- BootstrapContext = ci.BootstrapContext;
- foreach (var c in ci.Claims)
- this.claims.Add (c);
+
+ if (identity != null) {
+ if (string.IsNullOrEmpty (authenticationType))
+ auth_type = identity.AuthenticationType;
+
+ var ci = identity as ClaimsIdentity;
+ if (ci != null) {
+ actor = ci.Actor;
+ BootstrapContext = ci.BootstrapContext;
+ foreach (var c in ci.Claims)
+ this.claims.Add (c);
- foreach (var c in claims)
- this.claims.Add (c);
- Label = ci.Label;
- NameClaimType = ci.NameClaimType;
- RoleClaimType = ci.RoleClaimType;
- AuthenticationType = ci.AuthenticationType;
+ Label = ci.Label;
+ NameClaimType = string.IsNullOrEmpty (nameType) ? ci.NameClaimType : nameType;
+ RoleClaimType = string.IsNullOrEmpty (roleType) ? ci.RoleClaimType : roleType;
+ } else if (!string.IsNullOrEmpty (identity.Name)) {
+ AddDefaultClaim (identity.Name);
+ }
+ }
+
+ if (claims != null) {
+ AddClaims (claims);
}
}
return actor;
}
set {
- if (actor == this)
+ if (value == this)
throw new InvalidOperationException ("can not set the Actor property to this instance");
+
actor = value;
}
}
- public virtual string AuthenticationType { get; private set; }
+ public virtual string AuthenticationType {
+ get {
+ return auth_type;
+ }
+ }
public object BootstrapContext { get; set; }
public string Label { get; set; }
public virtual string Name {
{
if (claim == null)
throw new ArgumentNullException ("claim");
+
+ if (claim.Subject != this)
+ claim = claim.Clone (this);
+
claims.Add (claim);
}
{
if (claims == null)
throw new ArgumentNullException ("claims");
+
foreach (var c in claims)
- this.claims.Add (c);
+ AddClaim (c);
+ }
+
+ internal void AddDefaultClaim (string identityName)
+ {
+ this.claims.Add (new Claim (NameClaimType, identityName, "http://www.w3.org/2001/XMLSchema#string", DefaultIssuer, DefaultIssuer, this));
}
public virtual ClaimsIdentity Clone ()
yield return c;
}
- public virtual IEnumerable<Claim> FindAll(string type)
+ public virtual IEnumerable<Claim> FindAll (string type)
{
if (type == null)
throw new ArgumentNullException ("type");
foreach (var c in claims)
- if (c.Type == type)
+ if (string.Equals (c.Type, type, StringComparison.OrdinalIgnoreCase))
yield return c;
}
if (type == null)
throw new ArgumentNullException ("type");
foreach (var c in claims)
- if (c.Type == type)
+ if (string.Equals (c.Type, type, StringComparison.OrdinalIgnoreCase))
return c;
return null;
}
if (value == null)
throw new ArgumentNullException ("value");
foreach (var c in claims){
- if (c.Type == type && c.Value == value)
+ if (string.Equals (c.Type, type, StringComparison.OrdinalIgnoreCase) && c.Value == value)
return true;
}
return false;
}
}
}
-#endif
\ No newline at end of file
+#endif