C# 使用C将用户添加到Active Directory组时遇到问题#
好的,我现在的问题是,我们正在尝试编写代码,将用户添加到Active Directory中的其他组。这就是我们所写的解决方案 主要方法的一部分:C# 使用C将用户添加到Active Directory组时遇到问题#,c#,active-directory,windows-server-2008-r2,directoryservices,active-directory-group,C#,Active Directory,Windows Server 2008 R2,Directoryservices,Active Directory Group,好的,我现在的问题是,我们正在尝试编写代码,将用户添加到Active Directory中的其他组。这就是我们所写的解决方案 主要方法的一部分: string newGroup = "TestDelete"; string userName = result.Properties["cn"][0].ToString(); string adduser = ad.AddToGroup(userName, newGroup); Console.WriteLine(String.Format("{0}
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(字符串propertyName,对象[]args)
在C:\Users\XXX\Documents\Visual Studio 2010\Projects\UserPrunning\adjustUsers\Program.cs中的adjustUsers.Program.AddToGroup(String userDn,String groupDn)处:第45行
在C:\Users\XXX\Documents\Visual Studio 2010\Projects\UserPruning\UserPruning\MainProgram\MainProgram.cs中的UserPruning.MainProgram.Main(字符串[]args)处:第46行 据我们所知,这表明我们的语法有问题 第46行是
string adduser = ad.AddToGroup(userName,newGroup)
第45行是
ldapConnection.Invoke("Add", new object[] {newUser});
在最后一天,我们一直在试图重写这段代码,但仍然没有解决
帮忙
谢谢如果您使用的是.NET 3.5及更高版本,您应该查看
System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。请在此处阅读所有相关内容:
// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// find your user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "TestDelete");
// if found....
if (group != null)
{
// add user to group
group.Members.Add(user);
group.Save();
}
}
}
新的S.DS.AM使得在广告中与用户和群组进行互动变得非常容易 我希望我能不止一次投票支持这个答案。我觉得几乎所有我在网上找到的关于广告操纵的东西都是通过C#链接到Howto:(几乎)所有在Active Directory中通过C#找到的东西[文章,它使用DirectoryEntry API。DirectoryServices API更好!非常感谢!问题是,AccountManagement命名空间只有在运行它的计算机位于该域上并且可以解析您正在查询的域时才起作用。我正在查询另一个域。这可能是获取有关该域的异常信息的原因。”无法检索域(1355)。”?