2010-07-26 73 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) 
      { 

      } 
     }