Adding AD-user in AD-group

A

Andy_Alpha

Here's code,that i think,add user in group

....
DirectoryEntry grp=new DirectoryEntry(GroupName);
MessageBox.Show(GroupName,"group to add");
try
{
if (grp!=null)
{
grp.Invoke("Add",new object[] {User.Path.ToString()});}
grp.CommitChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"An Exception occured during adding to group");
}
}
....
GroupName is something like
LDAP://cn=Admin,ou=Administration,ou=main office,dc=beta-comp,dc.com

It generate exception:
"Exception has been thrown by the target of an invocation"
Help me please!
P.S. Very sorry for my english :)
 
M

Marc Scheuner [MVP ADSI]

Here's code,that i think,add user in group
...
DirectoryEntry grp=new DirectoryEntry(GroupName);
MessageBox.Show(GroupName,"group to add");
try
{
if (grp!=null)
{
grp.Invoke("Add",new object[] {User.Path.ToString()});}
grp.CommitChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"An Exception occured during adding to group");
}
}
...
GroupName is something like
LDAP://cn=Admin,ou=Administration,ou=main office,dc=beta-comp,dc.com

What's the user LDAP path look like??

You're trying to add a user to a group, right?

Why so complicated ?? Basically, you can do:

DirectoryEntry grp = new DirectoryEntry(GroupName);
try
{
if (grp!=null)
{
grp.Properties["member"].Add(strUserLDAPString);
grp.CommitChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"An Exception occured during adding
to group");
}
}

That should do the trick.

Check out the "System.DirectoryServices Portal" on MSDN:
http://msdn.microsoft.com/library/en-us/sds/sds/portal.asp

It contains TONS of samples in both VB.NET and C# - see here
http://msdn.microsoft.com/library/en-us/sds/sds/quick_list_for_c__code_examples.asp
(C#)
http://msdn.microsoft.com/library/en-us/sds/sds/quick_list_for_visual_basic__net_code_examples.asp
(VB.NET)

and tons of background info on S.DS- well worth a visit !

Marc
 
A

Andy_Alpha

Thank you very much for help,it's really works.
Marc Scheuner said:
Here's code,that i think,add user in group
...
DirectoryEntry grp=new DirectoryEntry(GroupName);
MessageBox.Show(GroupName,"group to add");
try
{
if (grp!=null)
{
grp.Invoke("Add",new object[] {User.Path.ToString()});}
grp.CommitChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"An Exception occured during adding to group");
}
}
...
GroupName is something like
LDAP://cn=Admin,ou=Administration,ou=main office,dc=beta-comp,dc.com

What's the user LDAP path look like??

You're trying to add a user to a group, right?

Why so complicated ?? Basically, you can do:

DirectoryEntry grp = new DirectoryEntry(GroupName);
try
{
if (grp!=null)
{
grp.Properties["member"].Add(strUserLDAPString);
grp.CommitChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"An Exception occured during adding
to group");
}
}

That should do the trick.

Check out the "System.DirectoryServices Portal" on MSDN:
http://msdn.microsoft.com/library/en-us/sds/sds/portal.asp

It contains TONS of samples in both VB.NET and C# - see here
http://msdn.microsoft.com/library/en-us/sds/sds/quick_list_for_c__code_examples.asp
(C#)
http://msdn.microsoft.com/library/en-us/sds/sds/quick_list_for_visual_basic__net_code_examples.asp
(VB.NET)

and tons of background info on S.DS- well worth a visit !

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top