2010-07-26 59 views
3

我要在活动目录中创建用于创建用户的web部件。在活动目录中创建用户

创建用户帐户我用这样的方法:

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; 
     newUser.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; 
} 

当执行这种方法发生了以下错误:

“的服务器不可操作”

回答

3

说,我们有活动目录与域安装,并且您有一个称为USERS的OU(组织单位),并且您有一个用户在其中称为TestUser

,所以我们可以萨耶以下

ldapDomain:完全合格的域名作为TestDomain.com或DC = CONTOSO,DC = COM
objectPath:完全合格的路径对象:CN = TestUser用户,OU =用户,DC = TESTDOMAIN,DC = com的
用户DN:用户的distinguishedName来:CN = TestUser用户,OU =用户,DC = TESTDOMAIN,DC = com的

在创建用户,你应该确定在哪里你想通过确定它的路径来创建(ld AP路径)

在我们的示例中,我们可以如下考虑:

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

有关更多信息,请访问以下链接:
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

1

使用的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) 
      { 

      } 
     } 

使用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) 
      { 

      } 
     }