我正在尝试使用以下代码将用户添加到组中。这在 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)
有人知道我做错了什么吗?