E
elziko
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_ACCOUNT 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
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_ACCOUNT 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