【发布时间】:2013-02-06 23:13:46
【问题描述】:
好的,我现在的问题是我们正在尝试编写将用户添加到我们 Active Directory 中不同组的代码。这是我们编写的解决方案。
部分main方法:
string newGroup = "TestDelete";
string userName = result.Properties["cn"][0].ToString();
string adduser = ad.AddToGroup(userName, newGroup);
Console.WriteLine(String.Format("{0} : {1}",userName, adduser));
从另一个类调用这个方法:
public String AddToGroup(string userDn, string groupDn)
{
try
{
DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://OU=" + groupDn + ",DC=blah,DC=blah,DC=blah");
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
string newUser = "CN=" + userDn + "CN=Members,DC=blah,DC=blah,DC=blah";
ldapConnection.Invoke("Add", new object[] { newUser });
ldapConnection.CommitChanges();
ldapConnection.Close();
return "Success";
}
catch (System.DirectoryServices.DirectoryServicesCOMException E)
{
Console.WriteLine("Exception caught:\n\n" + E.ToString());
}
}
抛出异常
System.Runtime.InteropServices.COMException (0x80020006):未知名称。 (来自 HRESULT 的异常:0x80020006 (DISP_E_UNKNOWNNAME))
在 System.DirectoryServices.DirectoryEntry.InvokeSet(String propertyName, Object[] args)
在 C:\Users\XXX\Documents\Visual Studio 2010\Projects\UserPruning\adjustUsers\Program.cs:line 45 中的 adjustUsers.Program.AddToGroup(String userDn, String groupDn)
在 C:\Users\XXX\Documents\Visual Studio 2010\Projects\UserPruning\UserPruning\MainProgram.cs: 46 行中的 UserPruning.MainProgram.Main(String[] args) 处
据我们所知,这表明我们的语法存在问题。
第 46 行是
string adduser = ad.AddToGroup(userName,newGroup)
第 45 行是
ldapConnection.Invoke("Add", new object[] {newUser});
我们在最后一天一直在尝试重写这段代码,但仍然感到困惑。
帮助?
谢谢
【问题讨论】:
标签: c# active-directory windows-server-2008-r2 directoryservices active-directory-group