473,385 Members | 1,331 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Creating a user using Active Directory

Hi,

I'm using the following code to create a user:

Dim strNodeName As String = "test user"
Dim NewUser As DirectoryEntry
Dim AD As New DirectoryEntry("WinNT://MYCOMPUTER")

'delete user when existing
Try
NewUser = AD.Children.Find(strNodeName, "User")
AD.Children.Remove(NewUser)
'catch 'not found' exception
Catch comEx As COMException
Console.WriteLine(comEx.Message)
End Try

'add user using the user schema
NewUser = AD.Children.Add(strNodeName, "user")
NewUser.Properties("description").Add("test user")

'set user flags, sets normal user and pwd cant change
NewUser.Properties("userFlags").Add(UF_NORMAL_ACCO UNT Or
UF_PASSWD_CANT_CHANGE)

'invoke native method 'Setpassword; before comitting
NewUser.Invoke("SetPassword", New Object() {"mysecret"})
NewUser.CommitChanges()

'add user toguest alias
Dim grp As DirectoryEntry = AD.Children.Find("Administrators", "group")
If Not grp.Name Is Nothing Then
grp.Invoke("Add", New Object() {NewUser.Path.ToString()})
Console.WriteLine("Account Created Successfully")
End If

This works fine if I set the domain to be the computer that the code runs on
but if I set it to be our companies domain then I get a
System.UnauthorizedAccessException. So I when instntiating 'AD' I aslo
passed in the username and password of the administrator on the domain who
is allowed to add users.

I then don;t get an exception, everything runs fine but no user is added to
the local machine, even after reboot. What am I doing wrong?

TIA

Jul 21 '05 #1
1 5169
>Dim strNodeName As String = "test user"
Dim AD As New DirectoryEntry("WinNT://MYCOMPUTER")
NewUser = AD.Children.Add(strNodeName, "user")
This works fine if I set the domain to be the computer that the code runs on
but if I set it to be our companies domain then I get a
System.UnauthorizedAccessException.


Well, first of all, if you have a "company domain", I would STRONGLY
suggest using the LDAP:// provider rather than the WinNT:// provider.
This WinNT codebae is really only provided as a backwards
compatibility mechanism for NT4 domains, and should be avoided
whenever possible. Also, the WinNT provider does NOT support your
Active Directory hierarchy - you can't create users in specific OU's
(since the WinNT provider only knows about a flat, non-hierarchical
model without OU's).

So I'd suggest using something like:

Dim AD As New DirectoryEntry("LDAP://cn=Users,dc=YourCOmpany,dc=com");
Dim NewUser as DirectoryEntry
NewUser = AD.Children.Add("cn=YourUserName", "user");
'' set properties for NewUser, then store them back to AD
NewUser.CommitChanges();

Secondly, in a corporate domain environment, your default user might
not have permissions to do such things as create a new user - you
possibly need to a) grant your user admin rights (at least on the OU
you're interested in), or b) use another user (and specify it in your
call to "New DirectoryEntry()".

Also, if you're in an ASP.NET environment, often this "serverless
binding" as mentioned above won't work, and you'd need to specify a
particular DC by machine name which should be used for your binding
operations:

Dim AD As New
DirectoryEntry("LDAP://DC01.YourCompan.com/cn=Users,dc=YourCOmpany,dc=com");
HTH
Marc
================================================== ==============
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: mcmg | last post by:
Hi, I have an asp app that works fine on a windows xp machine but does not work on a windows 2000 server. I have the following code in my global.asa: <OBJECT RUNAT=Server SCOPE=SESSION...
1
by: B. Zuidgeest | last post by:
I use C# to connect a database with student information directly to the Active Directory (2003). This means that as the student is enlisted an account and mailbox is created in the active...
2
by: Technical Group | last post by:
Friends, Can anybody help me out by sending a piece of C# code showing how to add an active directory user to a particular user group? If the group does not exist, then create it. Thanks in...
0
by: J'son | last post by:
Guys, I need to build a web intranet application that can automatically create a user account when a new user registers on the site. The user account will be on the web server, member of the...
6
by: Leo_Surf | last post by:
Hello, I need your help adding user in Active Directory from ASP.net website. Could any one provide me the complete code for the html page. As this is my curriculam project and I dont have any...
1
by: elziko | last post by:
Hi, I'm using the following code to create a user: Dim strNodeName As String = "test user" Dim NewUser As DirectoryEntry Dim AD As New DirectoryEntry("WinNT://MYCOMPUTER") 'delete user...
0
by: jakobsgaard | last post by:
It is possible to Map a certificate to a Active Directory User Account from DotNet? Please provide an example. Best regards, Ejnar Jakobsgaard...
1
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users...
7
by: Vio | last post by:
Hello everyone, i currently a beginner in php. I want to ask about Win2003 Active Directory users. Is it possible to retrieve Win2003 AD (just username & password) with php. I'm currenty...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.