【问题标题】:ASP: Reading Scripting.Dictionary with ADODB.Command ResultASP:使用 ADODB.Command 结果读取 Scripting.Dictionary
【发布时间】:2013-07-22 10:58:35
【问题描述】:

我正在尝试从 Active Directory 中查询 userAccountControl,并将其与我在脚本中设置的字典进行匹配。

首先我设置我的字典对象并填充它:

dim uac
Set uac=Server.CreateObject("Scripting.Dictionary")
uac.add "512", "Enabled Account"
uac.add "514", "Disabled Account"
uac.add "544", "Enabled, Password Not Required"
uac.add "546", "Disabled, Password Not Required"
uac.add "66048", "Enabled, Password Doesn't Expire"
uac.add "66050", "Disabled, Password Doesn't Expire"
uac.add "66080", "Enabled, Password Doesn't Expire & Not Required"
uac.add "66082", "Disabled, Password Doesn't Expire & Not Required"
uac.add "262656", "Enabled, Smartcard Required"
uac.add "262658", "Disabled, Smartcard Required"
uac.add "262688", "Enabled, Smartcard Required, Password Not Required"
uac.add "262690", "Disabled, Smartcard Required, Password Not Required"
uac.add "328192", "Enabled, Smartcard Required, Password Doesn't Expire"
uac.add "328194", "Disabled, Smartcard Required, Password Doesn't Expire"
uac.add "328224", "Enabled, Smartcard Required, Password Doesn't Expire & Not Required"
uac.add "328226", "Disabled, Smartcard Required, Password Doesn't Expire & Not Required"

然后我连接到我的 Active Directory 来查询它:

Set objDomain = GetObject ("GC://RootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.provider ="ADsDSOObject"
objConn.Properties("User ID") = "domain\administrator"
objConn.Properties("Password") = "password"
objConn.Properties("Encrypt Password") = True
objConn.open "Active Directory Provider"
Set objCom = CreateObject("ADODB.Command")
Set objCom.ActiveConnection = objConn
objCom.CommandText ="select name,userAccountControl FROM 'GC://"+objADsPath+"' where sAMAccountname='*' and objectCategory='user' and objectCategory='person' ORDER by sAMAccountname"

然后我像这样遍历结果:

Set objRS = objCom.Execute
Do While Not objRS.EOF Or objRS.BOF
Response.Write objRS("name")
set uacResult = objRS("userAccountControl")
objRS.MoveNext
Response.Flush
Loop
objRS.Close
objConn.Close
Set objRS = Nothing
Set objConn = Nothing
Set objCom = Nothing
Set objADsPath = Nothing
Set objDomain = Nothing

这一切都很好。我现在要做的是查询我使用来自 objRS("userAccountControl") 的结果创建的 uac 字典。当我尝试这样做时,它返回一个空白值?

set uacResult = objRS("userAccountControl")
Response.Write uac.Item(uacResult)
Response.Write " (" & uacResult & ")"

我很难过 - objRS 值是作为字符串返回的吗?如果我执行 Response.Write uac.Item("512") 它可以工作,但如果我执行 Response.Write uac.Item(uacResult) 则不行

有什么想法吗?

【问题讨论】:

    标签: asp-classic active-directory adodb ldap-query scripting.dictionary


    【解决方案1】:

    你已经找到了valid solution,但我想解释一下这里发生了什么。

    Dictionary 对象的键可以是数组以外的任何类型(请参阅docs)。由于您使用了Set 语句,因此变量uacResult 是一个对象引用(稍后会详细介绍)。当您在Dictionary 上调用Item 方法时,它实际上是在寻找与对象引用匹配的键,而不是像您预期的那样搜索字符串。 CStr() 通过将对象引用显式转换为字符串来纠正此问题。

    由于您在其赋值中使用了Set 关键字,因此uacResult 最终包含对Field 对象的引用,因为Recordset 对象的默认属性是Fields 集合。当您调用CStr(uacResult) 时,这是通过调用Field 对象的默认属性(即Value 属性)将对象转换为字符串。您可以通过几种不同的方式实现相同的效果:

    ' method 1 - from accepted answer (https://stackoverflow.com/a/17786308/249624)
    set uacResult = objRS("userAccountControl")
    Response.Write uac.Item(CStr(uacResult))
    
    ' method 2 - explicitly using the Value property
    set uacResult = objRS("userAccountControl")
    Response.Write uac.Item(uacResult.Value)
    
    ' method 3 - make uacResult a string instead of a Field object
    uacResult = objRS("userAccountControl")
    Response.Write uac.Item(uacResult)
    
    ' method 4 - same as method 3, but explicitly use the Value property
    uacResult = objRS("userAccountControl").Value
    Response.Write uac.Item(uacResult)
    

    【讨论】:

      【解决方案2】:

      找到了!!

      我需要使用 CStr() 将 objRS("userAccountControl") 转换为字符串。

      这行得通:

      set uacResult = objRS("userAccountControl")
      Response.Write uac.Item(CStr(uacResult))
      Response.Write " (" & uacResult & ")"
      

      【讨论】:

        猜你喜欢
        • 2015-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-10
        • 1970-01-01
        • 1970-01-01
        • 2010-10-13
        相关资源
        最近更新 更多