Esempio n. 1
0
        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
            })));
        }
Esempio n. 2
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);
        }
Esempio n. 3
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;
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
 private string GetName(InMemoryUser user)
 {
     return(user.Claims.GetValue(Constants.ClaimTypes.Name));
 }
Esempio n. 6
0
        private IdentityManagerResult SetUserProperty(IEnumerable <PropertyMetadata> propsMeta, InMemoryUser user, string type, string value)
        {
            IdentityManagerResult result;

            if (propsMeta.TrySet(user, type, value, out result))
            {
                return(result);
            }

            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);
            }

            return(IdentityManagerResult.Success);
        }
Esempio n. 7
0
 private IdentityManagerResult SetName(InMemoryUser user, string value)
 {
     user.Claims.SetValue(Constants.ClaimTypes.Name, value);
     return(IdentityManagerResult.Success);
 }
 private string GetName(InMemoryUser user)
 {
     return user.Claims.GetValue(Constants.ClaimTypes.Name);
 }
        private IdentityManagerResult SetUserProperty(IEnumerable<PropertyMetadata> propsMeta, InMemoryUser user, string type, string value)
        {
            IdentityManagerResult result;
            if (propsMeta.TrySet(user, type, value, out result))
            {
                return result;
            }

            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);
            }

            return IdentityManagerResult.Success;
        }
        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<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 }));
        }
 private IdentityManagerResult SetName(InMemoryUser user, string value)
 {
     user.Claims.SetValue(Constants.ClaimTypes.Name, value);
     return IdentityManagerResult.Success;
 }