Directory Service C# Lot of Points

I have this code which hopefully should create a new user on the gtdemo domain which is the domain i am logged on to.

However when it runs there are no errors but it does not create a new user, Can anyone help me figure this out.

Also is there a way to set up an exchange mailbox when creating a new user.

DirectoryEntry obDirEntry = null;

obDirEntry = new DirectoryEntry("WinNT://gtdemo");
DirectoryEntries entries = obDirEntry.Children;
DirectoryEntry obUser = entries.Add("Test", "User");
obUser.Properties["FullName"].Add("Test");
object obRet = obUser.Invoke("SetPassword", "Test");
obUser.CommitChanges();

Thanks

WVWAsked:
Who is Participating?
 
AerosSagaConnect With a Mentor Commented:
If you feel I helped you come to this conclusion with my inofrmation then award the points.  If you feel I did not aid in your coming to this conlcusion, then ask for a refund.

Aeros
0
 
AerosSagaCommented:
are you adding them to a group?

'Get object in ADs
Dim usr As DirectoryEntry = GetADsObject("person", loginUser, sUserProps, False)
Dim grp As DirectoryEntry = GetADsObject("group", groupName, sLoadProps, True)
If Not grp Is Nothing And Not usr Is Nothing Then
  'set authentication info for using ADs, feel free to create
  'a function for authenticating to AD.
  usr.AuthenticationType = AuthenticationTypes.Delegation
  usr.Username = "username"
  usr.Password = "password"
  grp.AuthenticationType = AuthenticationTypes.Delegation
  grp.Username = "username"
  grp.Password = "password"
  If Not IsMember(usr, grp) Then
    Dim strDisName As String = usr.Properties("distinguishedName").Value
    grp.Properties("Member").Add(strDisName)
    grp.CommitChanges()
  End If
End If

sorry for the vb:

http://www.wwwcoder.com/main/parentid/272/site/2150/68/default.aspx

Aeros
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
AerosSagaCommented:
Heres a prebuilt class for Acitive Directory Manipulation:
http://www.c-sharpcorner.com/Code/2002/Sept/ADClass.asp
0
 
WVWAuthor Commented:
What is a group and why do i need one to add users, i dont have any groups when i go to exchange users i click on the treeview for users and it displays them without a group
0
 
AerosSagaCommented:
are you just trying to add a user to exchange server?
0
 
AerosSagaCommented:
public void AddDirectoryObject(string AdsiParentPath,string ObjectName,
string ObjectSchemaName,object[,] Properties,object[,] MethodsToInvoke)
{
// connect to the selected directory parent object
DirectoryEntry DirEntry = new DirectoryEntry(AdsiParentPath);

try
{
// creates the new directory object
DirectoryEntry NewObject = DirEntry.Children.Add(ObjectName,ObjectSchemaName);

// now loop through all the properties and set them
if (Properties != null)
{
for (int Count = 0; Count < Properties.GetLength(0); Count++)
NewObject.Properties[Convert.ToString(Properties[Count,0])].
Value = Properties[Count,1];
}

// now loop through all the methods and invoke them
if (MethodsToInvoke != null)
{
for (int Count = 0; Count < MethodsToInvoke.GetLength(0); Count++)
NewObject.Invoke(Convert.ToString(MethodsToInvoke[Count,0]),
MethodsToInvoke[Count,1]);
}

// commit the changes
NewObject.CommitChanges();
NewObject.Close();
}

// catch any exception accessing the directory object
catch (Exception e)
{ }

// close the directory object
finally
{
DirEntry.Close();
}
}
http://www.c-sharpcorner.com/Code/2004/Aug/DirectoryServices.asp
0
 
WVWAuthor Commented:
I need to be able to add them to the active directory and assign a username and password so they can login and also i need to assign them an exchange mailbox as well.

The domain i need to logon to is gtdemo.

If anyone can give some insight into what code i need to use and how it works they can have the points.

Regards
0
 
WVWAuthor Commented:
I have put a tracer in and found why my code does not work, it says

"General access denied error\r\n"

This the code again, do i need to give it login credential to get to active directory

string strDoamin = "gtdemo";
                        string strLogin = "test";
                        string strPwd = "test";
                        DirectoryEntry obDirEntry = null;
                        try
                        {
                              obDirEntry = new DirectoryEntry("WinNT://" + strDoamin);
                              DirectoryEntries entries = obDirEntry.Children;
                              DirectoryEntry obUser = entries.Add(strLogin, "User");
                              obUser.Properties["FullName"].Add("Amigo");
                              object obRet = obUser.Invoke("SetPassword", strPwd);
                              obUser.CommitChanges();
                        }
                        catch (Exception ex)
                        {
                              Trace.Warn(ex.Message);
                        }
0
 
AerosSagaCommented:
private static string ADPath=ConfigurationSettings.AppSettings["ADFullPath"].ToString() ;
private static string ADUser = ConfigurationSettings.AppSettings["ADAdminUser"].ToString() ;
private static string ADPassword = ConfigurationSettings.AppSettings["ADAdminPassword"].ToString() ;

http://www.c-sharpcorner.com/Code/2002/Sept/ADClass.asp
0
 
WVWAuthor Commented:
Hi, i have got some code working which is as follows, however i could not get the code to insert the new user in the Users section of active directory instead it inserts in straight into the root node on active directory.

First person to modify my code to work so the user is inserted into the Users section get the points

Also a few question on line 3 it says user is that the acount type, and where can i find a list of mydirectoryEntry properties

Thanks

1. obDirEntry = new DirectoryEntry("LDAP://gtdemo","administrator","test");
2. DirectoryEntries myEntries = obDirEntry.Children;
3. DirectoryEntry myDirectoryEntry = myEntries.Add("CN=Test", "user");
4. myDirectoryEntry.Properties["name"].Value="Test";
5. myDirectoryEntry.Properties["samAccountName"].Value="Test";
6. myDirectoryEntry.CommitChanges();
0
 
WVWAuthor Commented:
I have solved the problem by using this code. Who feels they deserve the points, if you can justify why you can have them if not i will request a refund.

Thanks

obDirEntry = new DirectoryEntry ("LDAP://CN=Users,DC=gtdemo,DC=local" ,"administrator","test");
                              //obDirEntry = new DirectoryEntry("LDAP://gtdemo","administrator","test");
                  
                              DirectoryEntries myEntries = obDirEntry.Children;
                              
                              // Name of Logon
                              DirectoryEntry myDirectoryEntry = myEntries.Add("CN=JDixon", "user");
                              
                              //First Name
                              myDirectoryEntry.Properties["givenName"].Value="Jonathan";
                              //Surname
                              myDirectoryEntry.Properties["sn"].Value="Dixon";
                              // Logon Name
                              myDirectoryEntry.Properties["userPrincipalName"].Value="JDixon";
                              myDirectoryEntry.Properties["samAccountName"].Value="JDixon";
                              myDirectoryEntry.CommitChanges();
0
 
WVWAuthor Commented:
I like your honesty and i will be honest with you, it did not help much but you can have the points anyway.

Thanks for helping
0
 
AerosSagaCommented:
np, thanks for the points
0
All Courses

From novice to tech pro — start learning today.