Add User To Group in Active Directory using JNDI

From: zieg (zieg_at_gmx.at)
Date: 05/26/04


Date: 26 May 2004 02:56:08 -0700

Hi all!

I am new to Active directory and have Problems adding a new User to a
Distribution-Group in MS Active Direcory.

I created a custom group called "GR1" via the Windows GUI
Now i want to add/remove users to this group using java/jndi

I read that the "memberOf" attribute of the user can not be changed
instead you have to change the "member" attribute of the group the
user should be added. But when i try to modify the "member" attribute
of the group i get the following Exception:

javax.naming.NameNotFoundException: [LDAP: error code 32 - 0000208D:
NameErr: DSID-031001B8, problem 2001 (NO_OBJECT), data 0, best match
of: '' ]; remaining name 'CN=GR1,CN=Users'

When i try to change the "description" attribute of the group with the
same code it works fine.

Here is the code i use

import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;

public class Test2 {

    public static void main( String[] args )
    {
        // Set up environment for creating initial context
        Hashtable env = new Hashtable(11);
        NamingEnumeration ne;

        try {
            String SRV = args[0];
            String USER = args[1];
            String PWD = args[2];

            String UserDN = "CN=TestUser,CN=Users";
            String GroupDN = "CN=GR1,CN=Users";

            env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, SRV);
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, USER);
            env.put(Context.SECURITY_CREDENTIALS, PWD);

            // create initial context
            DirContext ctx = new InitialDirContext(env);

            ModificationItem[] mods = new ModificationItem[1];
            // mods[0] = new
ModificationItem(DirContext.REPLACE_ATTRIBUTE, new
BasicAttribute("description", "foobar")); // works fine
            // mods[0] = new
ModificationItem(DirContext.ADD_ATTRIBUTE,new
BasicAttribute("member",UserDN)); // does not work
            mods[0] = new
ModificationItem(DirContext.REPLACE_ATTRIBUTE,new
BasicAttribute("member", UserDN)); // does not work

            // try to set the member attribute
            try {
                ctx.modifyAttributes(GroupDN, mods);
            } catch (NamingException e) {
                e.printStackTrace();
            }

            // List the attributes of the group "GR1"
            String[] attrIDs = null;
            Attributes matchAttrs = new BasicAttributes(true);
//ignore case
            matchAttrs.put(new BasicAttribute("cn", "GR1"));
            SearchResult sr =
(SearchResult)ctx.search("CN=Users",matchAttrs,attrIDs).next();
            Attributes userAttrs = sr.getAttributes();

            for (NamingEnumeration ae = userAttrs.getAll();
ae.hasMore();) {
                Attribute attr = (Attribute)ae.next();
                System.out.print(attr.getID());
                for (NamingEnumeration e = attr.getAll(); e.hasMore();
System.out.println(" \"" + e.next().toString() + "\" "));
            }

            ctx.close();

        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}

Thanks a lot
Michael



Relevant Pages

  • Add User To Group in Active Directory using JNDI
    ... I am new to Active directory and have Problems adding a new User to a ... I created a custom group called "GR1" via the Windows GUI ... But when i try to modify the "member" attribute ... UserDN)); // does not work ...
    (comp.lang.java.programmer)
  • Add User To Group in Active Directory using JNDI
    ... I am new to Active directory and have Problems adding a new User to a ... I created a custom group called "GR1" via the Windows GUI ... But when i try to modify the "member" attribute ...
    (microsoft.public.win2000.active_directory)
  • Re: Inherited Permissions for Printers
    ... Yes it's possible to inheritance the Security from the ... active directory is child objects to it's host/computer/server. ... > 2000 Advanced Server member servers. ... > servers so that when a member of the domain security group "IT Helpdesk ...
    (microsoft.public.win2000.active_directory)
  • Re: Active Directory Only Displays Local Objects
    ... The SBS Server is the DC. ... up/restore Active Directory and restore it, ... necessitated a reinstall. ... Why can't the computer, which is a member of the domain, see Active ...
    (microsoft.public.windows.server.active_directory)
  • Re: Problem with domain name
    ... Single DC on the network. ... So I tried to add the member with this name and it worked. ... properties, under active directory on the DC, etc. ... It sounds like the DNS domain name is 'local" and the NetBIOS domain name ...
    (microsoft.public.windows.server.active_directory)