locked
C# creating active directory users RRS feed

  • Question

  • Hi All,

    I'm having a bit of trouble with something. I am currently using the DirectoryService.AccountManagement dll to create users in our AD server and everything works well. The below code creates the user as intended but for some reason it is not populating the default domain for the new user. Example if I look at the user I created using "AD users and Computers" and in the "Account" tab info

    "User Logon name" gets populated but the field beside it with the dropdown that contains the domains is blank. Is there a way to default that field example "@mydomain.com" or something in my code wrong? Thanks for any help!

      public UserPrincipal CreateNewUser(string sUserName, string sPassword, string sGivenName, string sSurname, string sEmailAddress, string sLanguage)
        {
            if (!IsUserExisiting(sUserName))
            {
                PrincipalContext oPrincipalContext = GetPrincipalContext();
    
                UserPrincipalsEx oUserPrincipalEx = new UserPrincipalsEx(oPrincipalContext, sUserName, sPassword, true);
                oUserPrincipalEx.UserPrincipalName = sUserName;
                oUserPrincipalEx.GivenName = sGivenName;
                oUserPrincipalEx.Surname = sSurname;
                oUserPrincipalEx.EmailAddress = sEmailAddress;
                oUserPrincipalEx.DisplayName = sGivenName + " " + sSurname;
                oUserPrincipalEx.Name = sGivenName + " " + sSurname;
                oUserPrincipalEx.preferredLanguage = sLanguage;
                oUserPrincipalEx.ExpirePasswordNow();
                oUserPrincipalEx.Save();
    
                return oUserPrincipalEx;
            }
            else
            {
                return GetUser(sUserName);
            }
        }

    
    
    Monday, July 16, 2012 7:45 PM

Answers

  • Hey thanks for the reply.

    I ended up solving this by just adding the domain and appended to the UserPrincipalName property value, so UserPrincipalName = "jsmith" + "@mydomain.com";

    
    
    
    • Marked as answer by Chochobeelai Tuesday, July 17, 2012 1:57 PM
    Tuesday, July 17, 2012 1:57 PM

All replies

  • I generated the code below based on the following wesite which I think should solve your problem

    http://www.codeproject.com/Articles/90142/Everything-in-Active-Directory-via-C-NET-3-5-Using

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.DirectoryServices.AccountManagement;
    namespace ConsoleApplication1
    {
        class Program
        {
            public static PrincipalContext oPrincipalContext;
            public static string sDomain = "test.com";
            public static string sDefaultOU = "OU=Test Users,OU=Test,DC=test,DC=com";
            public static string sServiceUser = @"ServiceUser";
            public static string sServicePassword = "ServicePassword";
            static void Main(string[] args)
            {
                oPrincipalContext = GetPrincipalContext();
            }
            public static PrincipalContext GetPrincipalContext()
            {
                PrincipalContext oPrincipalContext = new PrincipalContext
                   (ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind,
                   sServiceUser, sServicePassword);
                return oPrincipalContext;
            }
        }
    }


    jdweng

    Tuesday, July 17, 2012 1:14 AM
  • Hey thanks for the reply.

    I ended up solving this by just adding the domain and appended to the UserPrincipalName property value, so UserPrincipalName = "jsmith" + "@mydomain.com";

    
    
    
    • Marked as answer by Chochobeelai Tuesday, July 17, 2012 1:57 PM
    Tuesday, July 17, 2012 1:57 PM