Working with ADSI

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to write a program in VB.NET to work with Active Directory user accounts in my Win2K domain. I found sample code as shown below but when I add it to a project it says "Type IADsContainer is not defined" and "Type IADsUser is not defined". What is wrong? Am I missing a reference or something else?


Example: Creating a User Object (Visual Basic)

Dim Container as IADsContainer
Dim NewUser as IADsUser


‘ Bind to the known container.
Set Container = GetObject("WinNT://MSFT")

‘ Create the new Active Directory Service Interfaces User.
Set NewUser = Container.Create("User", "Jane")

‘ Set Jane’s password.
NewUser.AccountRestrictions.SetPassword("Argus")

‘ Complete the operation to create the object in the directory.
NewUser.SetInfo
 
Don't use the WinNT provider when binding to a AD domain.
And you should use the System.DirectoryServices namespace classes, the
encapsulate the COM/ADSI functionality.
Check the MSDN docs for details and samples.

Willy.


Dan Varozza said:
I am trying to write a program in VB.NET to work with Active Directory
user accounts in my Win2K domain. I found sample code as shown below but
when I add it to a project it says "Type IADsContainer is not defined" and
"Type IADsUser is not defined". What is wrong? Am I missing a reference or
something else?
 
I found sample code as shown below but when I add it to a project it says "Type IADsContainer is not defined" and
"Type IADsUser is not defined". What is wrong? Am I missing a reference or something else?

Yes - you didn't add a reference to the COM "Active Ds Type Library"
object to your project - but you don't really need to either.

Instead, use the System.DirectoryServices namespace and its classes.

And as Willy also pointed out, dump the WinNT provider - use LDAP
instead (since only LDAP knows about the OU hierarchies that are the
cornerstone of any AD system).

So your code would look something like that (it's in C#, since I'm not
intimiately familiar with the VB syntax, but you should be able to
convert it easily):

// set up container's LDAP path
string sContainerPath = "LDAP://cn=Users,dc=fabrikam,dc=com");

// bind to it
DirectoryEntry deCtr = new DirectoryEntry(sContainerPath);

// create new user, set sAMAccountName (mandatory)
DirectoryEntry deUser = deCtr.Children.Add("cn=Jane Doe", "user");
deUser.Properties["samAccountName"].Value = "Jane_Doe";

// save changes to AD store - you ned to do this *BEFORE*
// you set the password!
deUser.CommitChanges();

// set password
deUser.Invoke("SetPassword", new object[] {"secret"});

That should do.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Back
Top