开发者

Creating user in active directory

I'm gonna build a webpart for creating user in active directory .

For creating user account i use method like this :

public string CreateUserAccount(string ldapPath, string userName, 
    string userPassword)
{
    try
    {
        string oGUID = string.Empty;
        string connectionPrefix = "LDAP://" + ldapPath;
        DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
        DirectoryEntry newUser = dirEntry.Children.Add
            ("CN=" + userName, "user");
        newUser.Properties["samAccountName"].Value = userName;
        ne开发者_如何学PythonwUser.CommitChanges();
        oGUID = newUser.Guid.ToString();

        newUser.Invoke("SetPassword", new object[] { userPassword });
        newUser.CommitChanges();
        dirEntry.Close();
        newUser.Close();
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        //DoSomethingwith --> E.Message.ToString();

    }
    return oGUID;
}

When executing this method the following error occurred:

"The server is not operational"


say we have active directory installed with domain TestDomain.com and you have a OU ( Organization Unit ) called USERS and you have a user in it called TestUser

so we can saye the following

ldapDomain: the fully qualified domain as TestDomain.com or dc=contoso,dc=com
objectPath: the fully qualified path to the object: CN=TestUser, OU=USERS, DC=TestDomain, DC=com
userDn: the distinguishedName of the user: CN=TestUser, OU=USERS, DC=TestDomain, DC=com

in creating user you should determine where you want to create by determining its path ( ldap path )

In our sample we can consider it as below :

string ldapPath = "LDAP://OU=USERS, DC=TestDomain, DC=com"

For more information check the following links :
http://www.selfadsi.org/ldap-path.htm
http://www.informit.com/articles/article.aspx?p=101405&seqNum=7
http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.path.aspx


Using System.DirectoryServices

To use this namespace you need to add reference  System.DirectoryServices.dll 

       DirectoryEntry ouEntry = new DirectoryEntry("LDAP://OU=TestOU,DC=TestDomain,DC=local");

        for (int i = 3; i < 6; i++)
        {
            try
            {
                DirectoryEntry childEntry = ouEntry.Children.Add("CN=TestUser" + i, "user");
                childEntry.CommitChanges();
                ouEntry.CommitChanges();
                childEntry.Invoke("SetPassword", new object[] { "password" });
                childEntry.CommitChanges();
            }
            catch (Exception ex)
            {

            }
        }

Using System.DirectoryServices.AccountManagement

 To use this namespace you need to add reference  System.DirectoryServices.AccountManagement.dll 

              PrincipalContext ouContex = new PrincipalContext(ContextType.Domain, "TestDomain.local",           "OU=TestOU,DC=TestDomain,DC=local");

        for (int i = 0; i < 3; i++)
        {
            try
            {
                UserPrincipal up = new UserPrincipal(ouContex);
                up.SamAccountName = "TestUser" + i;
                up.SetPassword("password");
                up.Enabled = true;
                up.ExpirePasswordNow();
                up.Save();
            }
            catch (Exception ex)
            {

            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜