Solved

Directory Service C# Lot of Points

Posted on 2004-10-28
263 Views
Last Modified: 2008-01-09
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

0
Question by:WVW
    14 Comments
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    Heres a prebuilt class for Acitive Directory Manipulation:
    http://www.c-sharpcorner.com/Code/2002/Sept/ADClass.asp
    0
     

    Author Comment

    by:WVW
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    are you just trying to add a user to exchange server?
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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
     

    Author Comment

    by:WVW
    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
     

    Author Comment

    by:WVW
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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
     

    Author Comment

    by:WVW
    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
     

    Author Comment

    by:WVW
    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
     
    LVL 17

    Accepted Solution

    by:
    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
     

    Author Comment

    by:WVW
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    np, thanks for the points
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    845 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now