Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I'm trying to create a user and add it to 3 groups, the creation is working properly but adding user to groups sometimes it's working and sometimes show this error:

Quote:
there is no such object on the server


my code for adding user to group :

try
            {
                DirectoryEntry dirEntry = new DirectoryEntry("LDAP://" + group, ADUsername, ADPassword);
                if (dirEntry != null)
                {
                    dirEntry.Properties["member"].Add(userPrincipal.DistinguishedName);
                    dirEntry.CommitChanges();
                    dirEntry.Close();
                }
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                //doSomething with E.Message.ToString();

            }


What I have tried:

No Idea what's the problem and how to solve it
Posted
Updated 16-Jan-19 1:19am

Read this (especially the part about the LDAP url) : c# - Adding and removing users from Active Directory groups in .NET - Stack Overflow[^]
 
Share this answer
 
Comments
DevBassem 18-Sep-18 5:15am    
Thanx it's working!
I'm sure I tried it before it didn't work but now it's working properly
Use this code:
DirectoryEntry dirEntry = new DirectoryEntry("LDAP://" + group);
                if (dirEntry != null)
                {
                    dirEntry.Properties["member"].Add(userPrincipal.DistinguishedName);
                    dirEntry.CommitChanges();
                    dirEntry.Close();
                }


if the user have access to active directory it will work fine but if user dont have permission operation will failed
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900