经典ASP如何与Active Directory进行身份验证

5
我有一个经典的ASP网站(很抱歉!),其中的某些部分需要进行NT身份验证。 我希望呈现给用户一个美观的登录表单(而不是浏览器提示),然后根据AD对其进行身份验证,然后执行“如果成功则登录,如果失败则显示错误”的常规操作。这可能吗?我已在本地计算机上尝试了以下内容,但不确定如何正确测试成功或是否扩展到针对AD的搜索。
<html>
<head>
</head>
<body>
    <form action="test.asp" method="post">
        Username:
        <input type="text" name="strUserName"><br>
        Password:
        <input type="password" name="strPassword"><br>
        <input type="submit" name="btnSubmit">
    </form>
    <%
    If Request.Form("strUsername") <> "" Then
        Dim strADsPath
        strADsPath = "WinNT://ARIA"
        strUserName = Request.Form("strUserName")
        strPassword = Request.Form("strPassword")

        'Set adObject = GetObject("WinNT:")
        'Set userObject = adObject.OpenDSObject("WinNT://" & domainName, userName, password, ADS_SECURE_AUTHENTICATION)


        if (not strADsPath= "") then
            Dim oADsObject
            Set oADsObject = GetObject(strADsPath)

            response.write "Authenticating...<br><br>"

            Dim strADsNamespace
            Dim oADsNamespace

            strADsNamespace = left(strADsPath, instr(strADsPath, ":"))
            set oADsNamespace = GetObject(strADsNamespace)

            Set oADsObject = oADsNamespace.OpenDSObject(strADsPath, strUserName,strPassword, 0)

            if not (Err.number = 0) then
                Response.Write "<font color='red'><font size = 5><u><b>Authentication has failed...<b></u></font></font>"
                Session("Auth") = "NO"
            else
                Response.Write "<font color='blue'>USER AUTHENTICATED!</font><br>"
                Session("Auth") = "YES"
            end if
        end if
    End If
    %>
</body>
</html>

一旦身份验证成功,是否可以获取其他信息,如电子邮件和组?

我尝试了遵循Classic ASP (VBScript), 2008 R2, error using AD to authenticate并尝试对我的本地计算机进行身份验证,但无论我输入什么,它都始终进行身份验证。这是因为我使用本地计算机的原因吗?它就是行不通吗?


您需要启用“匿名身份验证”才能使其正常工作,因为您正在传递凭据,所以 IIS 不需要进行任何身份验证。请注意,此方法容易受到中间人攻击,我倾向于仅在封闭环境(如企业内部网络)中使用此方法,即使如此也不能保证100%的安全性。 - user692942
你能否详细解释一下“中间人攻击”的情况? - pee2pee
您想为Active Directory提供自己的用户界面进行身份验证,这意味着客户端将需要在HTML表单中提供其Windows用户名和密码,并以明文方式发布,通过在SSL连接上运行您的站点来保护此过程是必须的。 - user692942
1个回答

7
我知道这是一个老问题,但如果有人仍然感兴趣:
这是我如何通过经过身份验证的LDAP查询对用户进行身份验证的方法。如果查询失败,则不允许用户对域控制器进行身份验证。
这种方法有点不太优雅,因为它需要显式命名域控制器、域名(如果您想使用sam帐户名称)和搜索起始DN的OU。
  dim domainController : domainController = "yourdc.company.com"
  dim ldapPort : ldapPort = 389
  dim startOu : startOu = "DC=company,DC=com"

  Function CheckLogin( szUserName, szPassword)
    CheckLogin = False

    szUserName = trim( "" &  szUserName)

    dim oCon : Set oCon = Server.CreateObject("ADODB.Connection")
    oCon.Provider = "ADsDSOObject"
    oCon.Properties("User ID") = szUserName
    oCon.Properties("Password") = szPassword
    oCon.Open "ADProvider"
    dim oCmd : Set oCmd = Server.CreateObject("ADODB.Command")
    Set oCmd.ActiveConnection = oCon

    ' let's look for the mail address of a non exitsting user
    dim szDummyQuery : szDummyQuery = "(&(objectCategory=person)(samaccountname=DeGaullesC))"
    dim szDummyProperties : szDummyProperties = "mail"
    dim cmd : cmd = "<" & "LDAP://" & domainController & ":" & ldapPort & _
                        "/" & startOu & ">;" & szDummyQuery & ";" & szDummyProperties & ";subtree"
    oCmd.CommandText = cmd
    oCmd.Properties("Page Size") = 100
    on error resume next
    dim rs : Set rs = oCmd.Execute
    if err.Number = 0 then
      CheckLogin = true
      call rs.Close()
      set rs = nothing
    end if
    on error goto 0
    set oCmd = nothing
  End Function

  ' perform test
  dim res : res = CheckLogin( "youradname\youruser", "yourpassword")
  if res then
    Response.Write( "Login ok")
  else
    Response.Write( "Login failed")
  end if

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接