如何在VB.NET中设置用户“登录”AD属性

时间:2012-02-08 15:37:13

标签: vb.net active-directory

我正在努力升级基于Active Directory的VB.NET解决方案。截至目前,我正在尝试在创建用户时向新的AD用户添加PC限制。基本上,我需要更新Logon To属性以包含一台或多台PC,我该如何进行此操作?

我了解到我对IADsUser属性“LoginWorkstations”感兴趣(感谢http://msdn.microsoft.com/en-us/library/Aa746340)。截至目前,我的代码可以从任何AD用户获取此属性,但我无法设置它。

以下是我必须获取属性的代码:

Dim userADObject As new DirectoryEntry(ADPath)
Dim logonToPC as String = userADObject.InvokeGet("LoginWorkstations")(0).ToString

这将获取第一个受限制的PC(如果有的话)并将其保存在logonToPC中,看起来像“PC10000

这很有效,直觉上我会假设这样的东西会起作用:

Dim userADObject As new DirectoryEntry(ADPath)
Dim args() As Object = {"PC100001"}
userADObject.InvokeSet("LoginWorkstations", args)

但它不起作用......它只是抛出一个相当无益的例外。

我尝试使用不同的属性测试此方法,但它的工作正常。不幸的是,谷歌的数据并不多......

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

您应该可以相当轻松地执行此操作 - 另请注意:您应该使用userWorkstations LDAP属性(see note here) - 这是多值的,例如它允许多个条目。

Dim userADObject As new DirectoryEntry(ADPath)

userADObject.Properties("userWorkstations").Add("PC001")
userADObject.Properties("userWorkstations").Add("PC002")
userADObject.Properties("userWorkstations").Add("PC003")

userADObject.CommitChanges()

如果您具有更新Active Directory所需的权限,我认为基本上应该这样做。

答案 1 :(得分:0)

找到有效的解决方案。我拿了marc_s的代码并修改了一下才能正常工作。这就是我所拥有的:

Dim userADObject As New DirectoryEntry(Me.ADPath)
'Grab the previous restriction, because we may have to clear it first in the future
Dim priorRestriction As String = userADObject.Properties("userWorkstations").Value

If priorRestriction = "" Then
     'Simply add
     userADObject.Properties("userWorkstations").Add("PC001,PC002")
Else
     'Important - We have to clear the old restriction before adding the new
     userADObject.Properties("userWorkstations").Remove(priorRestriction)
     'Now add the new restriction
     userADObject.Properties("userWorkstations").Add(priorRestriction & ",PC003")
End If
'Commit!
userADObject.CommitChanges()

有些东西给了我一些非常好的悲伤,因为我不能在字符串中添加空格。示例:.Add("PC001, PC002") 必须 .Add("PC001,PC002")