2011-08-30 68 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 =确实有帮助。 –

相关问题