How do i set the user login name when creating a new user? RRS feed

  • Question

  • User-517928126 posted

     I am using the attached code to add a new ad user.  however, only the "pre windows 2000" user login name is being populated, the other user login name is blank ( i am using win2003 server, C#, visual studio 2008, .NET 3.5).  I found this list of attributes/values but i don't see anything for setting this.  Can someone help please?  http://msdn.microsoft.com/en-us/library/aa746340(VS.85).aspx  

    	objAD.Path = "WinNT://" + Environment.MachineName + ",Computer";
                    objAD.AuthenticationType = AuthenticationTypes.Secure;
                    objAD.Username = AdUser;
                    objAD.Password = AdPassword;
                    // create new user
                    using (objNewUser = objAD.Children.Add(user, "user"))
                        objNewUser.Invoke("SetPassword", new object[] { password });
                        objNewUser.Invoke("Put", new object[] { "Description", "WLAN Guest Access Account" });





    Wednesday, July 15, 2009 3:32 PM

All replies

  • User-517928126 posted

     Well here i am answering my own post again.  So either my questions are too dumb or too difficult - wish i knew how to get people to answer them.  Oh well, maybe this will help someone else like me in the future.  I finally found the attirbute i was looking for.  It is called UserPrinciple name. And i decided to use the System.DirectoryServices.AccountManagement assembly found in .NET 3.5.  It sure does make it easy.  here is a GREAT article i used...http://msdn.microsoft.com/en-us/magazine/cc135979.aspx

    and here is my code - which help explains what attirbutes match up with what...

    using  System.DirectoryServices.AccountManagement; // add reference
        public string AddUser3(string userName, string password, double hoursUntilExpiration)
            PrincipalContext machineContext = new PrincipalContext(ContextType.Domain, "yourDirectory.ca", AdUser, AdPassword);
            // create a user principal object
            UserPrincipal user = new UserPrincipal(machineContext, userName, password, true);
            // assign some properties to the user principal
            user.GivenName = <maps to first name in General Tab>
            user.Surname = <maps to last name in General Tab>
            user.Description = <maps to description seen in the AD users window>        
            user.UserPrincipalName = <User Login Name of Account Tab>
            user.SamAccountName = <pre win2000 User Login Name of Account Tab>
            // save the user to the directory
            return "success";





    Thursday, July 16, 2009 2:44 PM