www.kevincornwell.com
Create Active Directory User In .NET

C#

    public string CreateADUser(string szUsername, string szDisplayName, string szFirstName, string szLastName, string szMiddleInitial, string szPhone, string szEmail, string szOrgUnit)
    {
        string szPassword = "fooBar_123";  //temp only.
        string szCNName = "CN=" + szUsername;
 
        try
        {
            DirectoryEntry objContainer = new DirectoryEntry("LDAP://OU=mySubOU,OU=myMainOU,DC=KEVINCORNWELL,DC=com");
            DirectoryEntry objUser = objContainer.Children.Add(szCNName.ToLower(), "user");
            objUser.Properties["sAMAccountName"].Value = szUsername;
            objUser.Properties["userPrincipalName"].Value = szUsername + "@KEVINCORNWELL.COM";
            objUser.Properties["displayName"].Value = szDisplayName;
            objUser.Properties["GivenName"].Value = szFirstName;
            objUser.Properties["sn"].Value = szLastName;
            objUser.Properties["Initials"].Value = szMiddleInitial;
            objUser.Properties["mail"].Value = szEmail;
            objUser.Properties["TelephoneNumber"].Value = szPhone;
            objUser.Properties["Description"].Value = szOrgUnit;
            objUser.CommitChanges();
            objUser.Invoke("SetPassword", szPassword);
            objUser.CommitChanges();
            int flags = (int)objUser.Properties["userAccountControl"].Value;
            objUser.Properties["userAccountControl"].Value = flags & ~0x2;  //Enable User Account
            objUser.Properties["pwdLastSet"].Value = 0;
            objUser.CommitChanges();
            return "";
        }
        catch (Exception ex)
        {
            return ex.InnerException + ex.Message + ex.Source + ex.StackTrace + ex.Data;
        }
    }

Comments RSS


Leave a comment:


Blog | Contact | Gallery | Links | Music | Sandbox | Search | SharePoint | Social Networking | Software | Weather | Web Design


Copyright © 1997-2008 KCSH. All rights reserved.