且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在经典 ASP/VBScript 应用程序中通过 LDAP 访问员工 ID

更新时间:2023-09-21 13:11:52

我确信可能有更有效的方法可以做到这一点,但这是我经过多次搜索、尝试和咬牙切齿后最终使用的代码...

I'm sure there's probably a little more efficient way of doing this, but here's the code I ended up using after much searching, trying, and gnashing of teeth...

Dim strNTUser, strUser, strDN, strRootTDSE
Dim objRootDSE, objConnection, objCommand, objRecordSet, objUser, objNTUserInfo

strNTUser = Request.ServerVariables("AUTH_USER")
strUser = Mid(strNTUser,(instr(1,strNTUser,"")+1),len(strNTUser))

Set objConnection = Server.CreateObject("ADODB.Connection")
Set objCommand = Server.CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

'objCommand.Properties("Page Size") = 1000'
objCommand.Properties("Searchscope") = 2 'ADS_SCOPE_SUBTREE 

Set objRootDSE = GetObject("LDAP://rootDSE")
strRootTDSE = objRootDSE.Get("defaultNamingContext")
Set objRootDSE = Nothing

objCommand.CommandText = _
    "SELECT distinguishedName FROM 'LDAP://" & strRootTDSE & "' " & _
        "WHERE objectCategory='user' AND sAMAccountName = '" & strUser & "'" 

Set objRecordSet = objCommand.Execute

If Not objRecordSet.BOF Then objRecordSet.MoveFirst
If Not objRecordSet.EOF Then
    strDN = objRecordSet.Fields("distinguishedName").Value
End If

Set objConnection = Nothing
Set objCommand = Nothing
Set objRecordSet = Nothing

Set objUser = GetObject("LDAP://" & strDN)
'I can now use objUser to get the details'

我很乐意接受任何重构的代码,这也是我现在必须将网站降低到基本身份验证"以使其正常工作的原因.

I'll happily accept any refactored code, and a reason why I now have to lower the site to "Basic Authentication" in order for this to work.

附带说明一下,我尝试尽可能少地进行硬编码,以便将其发送回我从中获取原始代码的开源项目.

As a side note, I've tried to hard-code as little as possible so I can send it back to the open source project I got the original code from.