Exemplo n.º 1
0
        public void TestCustomUserAttributes()
        {
            var userData = CustomUserData.GenerateUserData("CustomCoreFxUser1");

            DeleteUser(userData.Name);

            try
            {
                using var context = DomainContext;
                using (var principal = CreateCustomUser(context, userData))
                {
                    Assert.NotNull(principal);
                    ValidateRecentAddedUser(context, userData);
                    ValidateUserUsingPrincipal(context, principal);

                    using var foundPrincipal = FindCustomUser(userData.Name, context);
                    Assert.NotNull(foundPrincipal);

                    Assert.Equal(userData.PostalCode, foundPrincipal.PostalCode);
                    Assert.Equal(principal.PostalCode, foundPrincipal.PostalCode);

                    Assert.Equal(userData.PostalAddress, foundPrincipal.PostalAddress);
                    Assert.Equal(principal.PostalAddress, foundPrincipal.PostalAddress);
                }
            }
            finally
            {
                DeleteUser(userData.Name);
            }
        }
Exemplo n.º 2
0
        public void TestMultiValueCustomAttributes()
        {
            var userData = CustomUserData.GenerateUserData("CustomCoreFxUser2");

            userData.PostalAddress.Add("Second address");

            DeleteUser(userData.Name);

            // Check whether directory-data is equivalent to expected data
            void CheckAddressWithDirectory(PrincipalContext context, List <string> address)
            {
                using var foundPrincipal = FindCustomUser(userData.Name, context);
                Assert.NotNull(foundPrincipal);
                Assert.Equal(address.ToHashSet(), foundPrincipal.PostalAddress.ToHashSet());
            };

            // Helper to update list
            void UpdateAddressList(CustomUserPrincipal principal, Action <List <string> > update)
            {
                var localCopy = principal.PostalAddress;

                update(localCopy);
                principal.PostalAddress = localCopy;
                principal.Save();
            }

            try
            {
                // Initial setup
                using var context   = DomainContext;
                using var principal = CreateCustomUser(context, userData);
                Assert.NotNull(principal);
                Assert.Equal(userData.PostalAddress, principal.PostalAddress);

                CheckAddressWithDirectory(context, principal.PostalAddress);

                // Add address
                UpdateAddressList(principal, addresses => addresses.Add("Third address"));
                CheckAddressWithDirectory(context, principal.PostalAddress);

                // Remove address
                UpdateAddressList(principal, addresses => addresses.Remove("Second address"));
                CheckAddressWithDirectory(context, principal.PostalAddress);

                // Remove address so we have one remaining
                UpdateAddressList(principal, addresses => addresses.Remove("Third address"));
                CheckAddressWithDirectory(context, principal.PostalAddress);
            }
            finally
            {
                DeleteUser(userData.Name);
            }
        }
Exemplo n.º 3
0
        private CustomUserPrincipal CreateCustomUser(PrincipalContext context, CustomUserData userData)
        {
            CustomUserPrincipal user = new CustomUserPrincipal(context, userData.Name, userData.Password, true);

            // assign some properties to the custom user principal
            user.GivenName     = userData.FirstName;
            user.Surname       = userData.LastName;
            user.DisplayName   = userData.DisplayName;
            user.PostalCode    = userData.PostalCode;
            user.PostalAddress = userData.PostalAddress;
            user.Save();
            return(user);
        }