public System.Threading.Tasks.Task <IdentityManagerResult <CreateResult> > CreateUserAsync(IEnumerable <PropertyValue> properties)
        {
            var errors = ValidateUserProperties(properties);

            if (errors.Any())
            {
                return(Task.FromResult(new IdentityManagerResult <CreateResult>(errors.ToArray())));
            }

            var user            = new InMemoryUser();
            var createPropsMeta = GetMetadata().UserMetadata.GetCreateProperties();

            foreach (var prop in properties)
            {
                var result = SetUserProperty(createPropsMeta, user, prop.Type, prop.Value);
                if (!result.IsSuccess)
                {
                    return(Task.FromResult(new IdentityManagerResult <CreateResult>(result.Errors.ToArray())));
                }
            }

            users.Add(user);

            return(Task.FromResult(new IdentityManagerResult <CreateResult>(new CreateResult()
            {
                Subject = user.Subject
            })));
        }
Ejemplo n.º 2
0
        private void SetUserProperty(IEnumerable <PropertyMetadata> propsMeta, InMemoryUser user, string type, string value)
        {
            if (propsMeta.TrySet(user, type, value))
            {
                return;
            }

            switch (type)
            {
            case "role.admin":
            {
                var val = Boolean.Parse(value);
                if (val)
                {
                    user.Claims.AddClaim(Constants.ClaimTypes.Role, "admin");
                }
                else
                {
                    user.Claims.RemoveClaim(Constants.ClaimTypes.Role, "admin");
                }
            }
            break;

            case "gravatar":
            {
                user.Claims.SetValue("gravatar", value);
            }
            break;

            default:
                throw new InvalidOperationException("Invalid Property Type : " + type);
            }
        }
        public static ICollection<InMemoryUser> Get(int random = 0)
        {
            var users = new HashSet<InMemoryUser>
            {
                new InMemoryUser{
                    Subject = Guid.Parse("081d965f-1f84-4360-90e4-8f6deac7b9bc").ToString(),
                    Username = "******",
                    Password = "******",
                    Email = "*****@*****.**",
                    Mobile = "123",
                    Claims = new HashSet<Claim>{
                        new Claim(Constants.ClaimTypes.Name, "Alice Smith"),
                        new Claim(Constants.ClaimTypes.Role, "admin"),
                        new Claim(Constants.ClaimTypes.Role, "employee"),
                        new Claim(Constants.ClaimTypes.Role, "manager"),
                        new Claim("department", "sales"),
                    }
                },
                new InMemoryUser{
                    Subject = Guid.Parse("5f292677-d3d2-4bf9-a6f8-e982d08e1306").ToString(),
                    Username = "******",
                    Password = "******",
                    Email = "*****@*****.**",
                    Claims = new HashSet<Claim>{
                        new Claim(Constants.ClaimTypes.Name, "Bob Smith"),
                        new Claim(Constants.ClaimTypes.Role, "employee"),
                        new Claim(Constants.ClaimTypes.Role, "developer"),
                        new Claim("department", "IT"),
                    }
                },
            };

            for (var i = 0; i < random; i++)
            {
                var user = new InMemoryUser
                {
                    Username = GenName().ToLower()
                };
                user.Claims.Add(new Claim("name", GenName() + " " + GenName()));
                users.Add(user);
            }

            return users;
        }
Ejemplo n.º 4
0
        public static ICollection <InMemoryUser> Get(int random = 0)
        {
            var users = new HashSet <InMemoryUser>
            {
                new InMemoryUser {
                    Subject  = Guid.Parse("081d965f-1f84-4360-90e4-8f6deac7b9bc").ToString(),
                    Username = "******",
                    Password = "******",
                    Email    = "*****@*****.**",
                    Mobile   = "123",
                    Claims   = new HashSet <Claim> {
                        new Claim(Constants.ClaimTypes.Name, "Alice Smith"),
                        new Claim(Constants.ClaimTypes.Role, "admin"),
                        new Claim(Constants.ClaimTypes.Role, "employee"),
                        new Claim(Constants.ClaimTypes.Role, "manager"),
                        new Claim("department", "sales"),
                    }
                },
                new InMemoryUser {
                    Subject  = Guid.Parse("5f292677-d3d2-4bf9-a6f8-e982d08e1306").ToString(),
                    Username = "******",
                    Password = "******",
                    Email    = "*****@*****.**",
                    Claims   = new HashSet <Claim> {
                        new Claim(Constants.ClaimTypes.Name, "Bob Smith"),
                        new Claim(Constants.ClaimTypes.Role, "employee"),
                        new Claim(Constants.ClaimTypes.Role, "developer"),
                        new Claim("department", "IT"),
                    }
                },
            };

            for (var i = 0; i < random; i++)
            {
                var user = new InMemoryUser
                {
                    Username = GenName().ToLower()
                };
                user.Claims.Add(new Claim("name", GenName() + " " + GenName()));
                users.Add(user);
            }

            return(users);
        }
        private string GetUserProperty(PropertyMetadata property, InMemoryUser user)
        {
            string value;

            if (property.TryGet(user, out value))
            {
                return(value);
            }

            switch (property.Type)
            {
            case "role.admin":
                return(user.Claims.HasValue(Constants.ClaimTypes.Role, "admin").ToString().ToLower());

            case "gravatar":
                return(user.Claims.GetValue("gravatar"));
            }

            throw new Exception("Invalid property type " + property.Type);
        }
 private void SetName(InMemoryUser user, string value)
 {
     user.Claims.SetValue(Constants.ClaimTypes.Name, value);
 }
 private string GetName(InMemoryUser user)
 {
     return user.Claims.GetValue(Constants.ClaimTypes.Name);
 }
        private void SetUserProperty(IEnumerable<PropertyMetadata> propsMeta, InMemoryUser user, string type, string value)
        {
            if (propsMeta.TrySet(user, type, value))
            {
                return;
            }

            switch (type)
            {
                case "role.admin":
                    {
                        var val = Boolean.Parse(value);
                        if (val)
                        {
                            user.Claims.AddClaim(Constants.ClaimTypes.Role, "admin");
                        }
                        else
                        {
                            user.Claims.RemoveClaim(Constants.ClaimTypes.Role, "admin");
                        }
                    }
                    break;
                case "gravatar":
                    {
                        user.Claims.SetValue("gravatar", value);
                    }
                    break;
                default:
                    throw new InvalidOperationException("Invalid Property Type : " + type);
            }
        }
 private string GetUserProperty(PropertyMetadata property, InMemoryUser user)
 {
     string value;
     if (property.TryGet(user, out value))
     {
         return value;
     }
     
     switch (property.Type)
     {
         case "role.admin":
             return user.Claims.HasValue(Constants.ClaimTypes.Role, "admin").ToString().ToLower();
         case "gravatar":
             return user.Claims.GetValue("gravatar");
     }
    
     throw new Exception("Invalid property type " + property.Type);
 }
        public System.Threading.Tasks.Task<IdentityManagerResult<CreateResult>> CreateUserAsync(IEnumerable<Property> properties)
        {
            var errors = ValidateUserProperties(properties);
            if (errors.Any())
            {
                return Task.FromResult(new IdentityManagerResult<CreateResult>(errors.ToArray()));
            }

            var user = new InMemoryUser();
            var createPropsMeta = GetMetadata().UserMetadata.GetCreateProperties();
            foreach(var prop in properties)
            {
                SetUserProperty(createPropsMeta, user, prop.Type, prop.Value);
            }
            
            users.Add(user);

            return Task.FromResult(new IdentityManagerResult<CreateResult>(new CreateResult() { Subject = user.Subject }));
        }
 private string GetName(InMemoryUser user)
 {
     return(user.Claims.GetValue(Constants.ClaimTypes.Name));
 }
 private IdentityManagerResult SetName(InMemoryUser user, string value)
 {
     user.Claims.SetValue(Constants.ClaimTypes.Name, value);
     return(IdentityManagerResult.Success);
 }
Ejemplo n.º 13
0
 private void SetName(InMemoryUser user, string value)
 {
     user.Claims.SetValue(Constants.ClaimTypes.Name, value);
 }