Example #1
0
 /// <summary>
 /// Adds a claim to a given identity and specify one or more destinations.
 /// </summary>
 /// <param name="identity">The identity.</param>
 /// <param name="type">The type associated with the claim.</param>
 /// <param name="value">The value associated with the claim.</param>
 /// <param name="destinations">The destinations associated with the claim.</param>
 public static ClaimsIdentity AddClaim(this ClaimsIdentity identity,
     string type, string value, params string[] destinations)
 {
     // Note: guarding the destinations parameter against null values
     // is not necessary as AsEnumerable() doesn't throw on null values.
     return identity.AddClaim(type, value, destinations.AsEnumerable());
 }
        /// <summary>
        /// 基于Claims-based的认证 
        /// </summary>
        /// <param name="identity">identity</param>
        /// <param name="id">登录Id</param>
        /// <param name="name">登录名</param>
        /// <param name="roles">角色集合</param>
        /// <returns></returns>
        public static ClaimsIdentity SetClaimsIdentity(this ClaimsIdentity identity, string id, string loginName, string realName, string[] roles)
        {
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, id));
            identity.AddClaim(new Claim(ClaimTypes.Name, loginName));
            identity.AddClaim(new Claim(ClaimTypes.Surname, realName));
            if (roles != null)
                foreach (var item in roles)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        identity.AddClaim(new Claim(ClaimTypes.Role, item));
                    }
                }
            identity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2015/11/claims/identityprovider", "ASP.NET Identity"));

            return identity;
        }
        public static void SetValue(this ICollection<Claim> claims, string type, string value)
        {
            if (claims == null) throw new ArgumentNullException("type");
            if (String.IsNullOrWhiteSpace(type)) throw new ArgumentNullException("type");

            claims.RemoveClaim(type);

            if (!String.IsNullOrWhiteSpace(value))
            {
                claims.AddClaim(type, value);
            }
        }
 public static ClaimsIdentity AddClaimCustom(this ClaimsIdentity identity, string type, string value, string issuer)
 {
     if( identity==null)
     {
         throw new ArgumentNullException("Identity is null");
     }
     if(!string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(value))
     {
         identity.AddClaim(new Claim(type, value, ClaimValueTypes.String, issuer?? ClaimsIdentity.DefaultIssuer));
     }
     return identity;
 }
        public static void SetIsPersistent(this ClaimsIdentity identity, bool isPersistent)
        {
            var claim = identity.Claims.FirstOrDefault(c => c.Type == PersistentLoginClaimType);

            if (isPersistent && claim == null)
            {
                identity.AddClaim(new Claim(PersistentLoginClaimType, bool.TrueString));
            }
            else if (claim != null)
            {
                identity.RemoveClaim(claim);
            }
        }
Example #6
0
		private static void AddCourseRole(this ClaimsIdentity identity, string courseId, CourseRole role) 
		{
			identity.AddClaim(new Claim(courseRoleClaimType, courseId + " " + role));
		}
 public static void AddImpersonatorId(this ClaimsIdentity identity, Guid id)
 {
     identity.AddClaim(new Claim(Type, id.ToString()));
 }
        /// <summary>
        /// Adds a claim to a given identity.
        /// </summary>
        /// <param name="identity">The identity.</param>
        /// <param name="type">The type associated with the claim.</param>
        /// <param name="value">The value associated with the claim.</param>
        /// <param name="destination">The destination associated with the claim.</param>
        public static ClaimsIdentity AddClaim(this ClaimsIdentity identity, string type, string value, string destination) {
            if (identity == null) {
                throw new ArgumentNullException(nameof(identity));
            }

            identity.AddClaim(new Claim(type, value).WithDestination(destination));
            return identity;
        }
Example #9
0
        /// <summary>
        /// Adds a claim to a given identity and specify one or more destinations.
        /// </summary>
        /// <param name="identity">The identity.</param>
        /// <param name="type">The type associated with the claim.</param>
        /// <param name="value">The value associated with the claim.</param>
        /// <param name="destinations">The destinations associated with the claim.</param>
        public static ClaimsIdentity AddClaim(this ClaimsIdentity identity,
            string type, string value, IEnumerable<string> destinations)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            if (destinations == null)
            {
                throw new ArgumentNullException(nameof(destinations));
            }

            identity.AddClaim(new Claim(type, value).SetDestinations(destinations));
            return identity;
        }