c# - How do I apply a new group to a user in an OU in Microsoft Active Directory -
my system has users login register/renew accounts. in end set group , check if there. changed our system , move users new ou if renewing (already in ou).
what want apply new group user when renew or register. resulting in user being member of 2 groups.
directoryentry instructorroot = new directoryentry(ldap_ou_dir); //root binding instructorroot.authenticationtype = authenticationtypes.signing | authenticationtypes.secure | authenticationtypes.sealing | authenticationtypes.fastbind; directoryentry instructor = new directoryentry(ldaproot); //default value instructor.authenticationtype = instructorroot.authenticationtype; /*here im trying testgroup group*/ directoryentry instructorgroup = instructorroot.children.find("cn="+testgroup, "group"); instructorgroup.authenticationtype = instructorroot.authenticationtype;
instructor = instructorroot.children.add("cn=" + hfuser.value, "user"); instructor.commitchanges(); instructor.properties["userprincipalname"].value = hfuser.value + "@" + ldaproot; instructor.properties["samaccountname"].value = hfuser.value; //login name instructor.commitchanges(); /*here im trying add insttest group user*/ instructorgroup.properties["member"].add(instructor.properties["distinguishedname"].value); //add instructors group instructorgroup.commitchanges(); //commit changes can set primary group next instructorgroup.close();//close instructor.properties["primarygroupid"].value = 109929; //set primarygroup instructors instructor.commitchanges();
private void addmembertogroup(string bindstring, string newmember) { try { directoryentry ent = new directoryentry(bindstring); ent.properties["member"].add(newmember); ent.commitchanges(); } catch (exception e) { // error catching stuff here return; } }
where bindstring string contains full directory new group want add users to.
and
newmember string obtained user object's .properties["distinguishedname"].value.tostring() method.