将用户添加到Active Directory中的组

时间:2013-06-26 12:19:20

标签: vb.net

我正在尝试使用以下代码将用户添加到组中。这在Windows 8计算机上运行时可以正常工作,但在Windows 7上运行时或在服务器上本地运行时都可以正常工作。

Dim userContext As New PrincipalContext(ContextType.Domain, domainName, userDistinguishedName)
Dim groupContext As New PrincipalContext(ContextType.Domain, domainName, groupDistinguishedName)

Dim user As UserPrincipal = UserPrincipal.FindByIdentity(userContext, IdentityType.SamAccountName, samAccountName)
Dim group As GroupPrincipal = GroupPrincipal.FindByIdentity(groupContext, groupName)
group.Members.Add(user)
group.Save()

变量看起来像:

samAccountName: qwerty
groupName: Domain Guests
domainName: testdomain
userDistinguishedName: OU=Staff Member Accounts,OU=Domain Accounts,OU=Domain Objects,DC=testdomain,DC=local
groupDistinguishedName: CN=Users,DC=testdomain,DC=local

错误是:

System.ArgumentNullException: Value cannot be null.
Parameter name: pricipal
 at
System.DirectoryServices.AccountManagement.PrincipalCollection.add(Principal Principal)
 at
System.DirectoryServices.AccountManagement.PrincipalCollection.add(User Principal User)
 at
Add_New_User_Module.addNewUserToGroup(String samAccountName, String groupName, String domainName, String userDistinguishedName, String groupDistinguishedName)

任何人都有任何想法,我做错了什么?

0 个答案:

没有答案