Example #1
0
        /// <summary>
        /// Rename AD user.
        /// </summary>
        /// <param name="ldapConnectionInfo">Properties to define LDAP connection</param>
        /// <param name="userProperties">Properties to define the user to be renamed</param>
        /// <returns>operationSuccessful = true if operation is ok.</returns>
        public static OutputUser AD_RenameUser([PropertyTab] LdapConnectionInfo ldapConnectionInfo, [PropertyTab] AD_RenameUserProperties userProperties)
        {
            var ldapOperationResult = new OutputUser {
                OperationSuccessful = false, User = null
            };

            List <DirectoryEntry> tmpObjectEntries;

            ldapConnectionInfo.LdapUri = ldapConnectionInfo.LdapUri + "/" + userProperties.Path;

            var filter = "(&(objectClass=user)(cn=" + userProperties.Cn + "))";

            // @"(&(objectClass=user)(cn=MattiMeikalainen))
            using (var ldap = new LdapService(ldapConnectionInfo))
            {
                tmpObjectEntries = ldap.SearchObjectsByFilter(filter);
                if (tmpObjectEntries.Count == 1)
                {
                    ldapOperationResult.User = ldap.RenameAdUser(tmpObjectEntries[0], userProperties.NewCn);
                }
                else if (tmpObjectEntries.Count == 0)
                {
                    throw new Exception($"Did not find any entries matching filter {filter} from {ldapConnectionInfo.LdapUri}");
                }
                else if (tmpObjectEntries.Count > 1)
                {
                    throw new Exception($"Found more than one entry matching filter {filter} from {ldapConnectionInfo.LdapUri}");
                }
            }

            ldapOperationResult.OperationSuccessful = true;
            return(ldapOperationResult);
        }
        /// <summary>
        /// Update a user in the AD.
        /// </summary>
        /// <param name="ldapConnectionInfo">The LDAP connection information</param>
        /// <param name="adUser">The user record to be updated</param>
        /// <returns>LdapResult class, which carries a copy of the updated user record.</returns>
        public static OutputUser AD_UpdateUser([PropertyTab] LdapConnectionInfo ldapConnectionInfo, [PropertyTab] UpdateADuser adUser)
        {
            var ldapOperationResult = new OutputUser {
                operationSuccessful = false, user = null
            };

            using (var ldap = new LdapService(ldapConnectionInfo))
            {
                ldapOperationResult.user = ldap.UpdateAdUser(adUser);
                ldapOperationResult.operationSuccessful = true;

                return(ldapOperationResult);
            }
        }
        /// <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);
            }
        }