2011-08-30 67 views
0

我正在嘗試編寫一個程序,該程序會使用來自外部數據源的數據自動創建活動目錄帳戶。我遇到的問題是,我總是得到一個UnAuthorizedAccessException,但是我終身無法考慮應用什麼權限。我甚至一直走到根對象,並給予我自己的帳戶完全控制,這似乎沒有任何區別。我知道我可以訪問服務器,因爲organizationUnit和de對象正確填充。通過代碼添加Active Directory帳戶所需的權限

DirectoryEntry de = new DirectoryEntry("LDAP://MYLOCALADDRESS");    
de.Password = "thePassword"; 
de.Username = "theUserName"; 
de.AuthenticationType = AuthenticationTypes.Secure ; 
DirectoryEntry organizationalUnit = de.Parent; 
DirectoryEntry newUser = organizationalUnit.Children.Add("TESTADD ", de.SchemaClassName); 

//Exception happens on this line 
newUser.CommitChanges(); 

任何幫助,將不勝感激!

回答

1

一覽我說你的「TESTADD」需要先從「CN =」

的Active Directory我從中得到codeproject我所有的樣本:

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; 
} 
+0

終於想通了,我試圖添加到我的生產活動目錄中,而不是我設置的測試。然後你的提示與cn =確實有幫助。 –

相關問題