473,586 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Fin d(strNodeName, "User")
AD.Children.Rem ove(NewUser)
'catch 'not found' exception
Catch comEx As COMException
Console.WriteLi ne(comEx.Messag e)
End Try

'add user using the user schema
NewUser = AD.Children.Add (strNodeName, "user")
NewUser.Propert ies("descriptio n").Add("tes t user")

'set user flags, sets normal user and pwd cant change
NewUser.Propert ies("userFlags" ).Add(UF_NORMAL _ACCOUNT Or
UF_PASSWD_CANT_ CHANGE)

'invoke native method 'Setpassword; before comitting
NewUser.Invoke( "SetPasswor d", New Object() {"mysecret"} )
NewUser.CommitC hanges()

'add user toguest alias
Dim grp As DirectoryEntry = AD.Children.Fin d("Administrato rs", "group")
If Not grp.Name Is Nothing Then
grp.Invoke("Add ", New Object() {NewUser.Path.T oString()})
Console.WriteLi ne("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.Unauthor izedAccessExcep tion. 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 5198
>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.Unautho rizedAccessExce ption.


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=You rCOmpany,dc=com ");
Dim NewUser as DirectoryEntry
NewUser = AD.Children.Add ("cn=YourUserNa me", "user");
'' set properties for NewUser, then store them back to AD
NewUser.CommitC hanges();

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=You rCOmpany,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
3987
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 ID=MyID
1
1945
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 directory. Creating users is flawless en mailbox creating went perfect against a single exhange server using code from KB article 313114 of which part is...
2
6602
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 advance -Hari
0
262
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 guests group (or some other limited group), and get read permissions to a special folder. I've tried playing with Active Directory on my local box,...
6
2403
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 Idea about ASP.net Please Help Thanks in Advance.
1
336
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 when existing Try
0
2733
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 ------------------------------------------------- To map a certificate to a user account Open Active Directory Users and Computers.
1
1960
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 are created but the account is disabled (see the flag User.AccountDisabled = False ). There is also another problem even if the user does not exist...
7
2774
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 developing web based application for my small office. I put my Apache+php+MySQL in Win2003 Server. My Clients using IE as default browser. What i want to...
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.