Esempio n. 1
0
        private void SetProperty(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);
        }
Esempio n. 3
0
        private string GetProperty(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. 4
0
        public System.Threading.Tasks.Task <IdentityManagerResult <CreateResult> > CreateUserAsync(IEnumerable <UserClaim> properties)
        {
            var errors = ValidateProperties(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)
            {
                SetProperty(createPropsMeta, user, prop.Type, prop.Value);
            }

            users.Add(user);

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