public DirectoryEntry CreateAdUser(CreateADuser user)
        {
            var entryAttributes = GetEntryAttributes(user, user.OtherAttributes);

            if (!user.CN.ToUpper().StartsWith("CN="))
            {
                user.CN = "CN=" + user.CN;
            }

            var entry = CreateEntry(user.CN, user.Path, LdapClasses.User, entryAttributes);

            SaveEntry(entry);

            foreach (var flag in user.ADFlags)
            {
                entry.SetAccountFlag(flag.FlagType, flag.Value);
            }

            SaveEntry(entry);
            return(entry);
        }
Exemplo n.º 2
0
        public void ShouldCreateUser()
        {
            var user = new CreateADuser
            {
                CN   = _user,
                Path = _path
            };
            var attributes = new List <EntryAttribute> {
                new EntryAttribute()
                {
                    Attribute = AdUserAttribute.givenName, Value = "Matti", DataType = AttributeType.String
                }
            };

            user.OtherAttributes = attributes.ToArray();

            var flags = new List <ADFlag>
            {
                new ADFlag {
                    FlagType = ADFlagType.ADS_UF_ACCOUNTDISABLE, Value = false
                },
                new ADFlag {
                    FlagType = ADFlagType.ADS_UF_NORMAL_ACCOUNT, Value = true
                }
            };

            user.ADFlags = flags.ToArray();

            var e = new AD_CreateUserProperties()
            {
                newPassword = "",
                setPassword = false
            };
            var result = LdapActiveDirectoryOperations.AD_CreateUser(_connection, user, e);

            Assert.AreEqual(result.operationSuccessful, true);
        }
        /// <summary>
        /// Create a user to AD.
        /// </summary>
        /// <param name="ldapConnectionInfo">The LDAP connection information</param>
        /// <param name="adUser">The user record to be created</param>
        /// <param name="Password">Passes two parameters to this task: bool setPassword, which defines if a password should be set at create time, and string newPassword, containing the password to be set.</param>
        /// <returns>LdapResult class, which carries a copy of the created user record.</returns>
        public static OutputUser AD_CreateUser([PropertyTab] LdapConnectionInfo ldapConnectionInfo, [PropertyTab] CreateADuser adUser, AD_CreateUserProperties Password)
        {
            var ldapOperationResult = new OutputUser {
                operationSuccessful = false, user = null
            };

            using (var ldap = new LdapService(ldapConnectionInfo))
            {
                ldapOperationResult.user = ldap.CreateAdUser(adUser);

                if (Password.setPassword)
                {
                    SetPassword.SetUserPassword(ldapConnectionInfo.LdapUri, adUser.Path, ldapConnectionInfo.Username, ldapConnectionInfo.Password, adUser.CN, Password.newPassword);
                }

                ldapOperationResult.operationSuccessful = true;

                return(ldapOperationResult);
            }
        }