2014-06-09 3 views
0

Visual Basic (VB6)에 응용 프로그램이 있으며 Active Directory로 사용자를 인증하려고합니다.VB6에서 Active Directory 사용자를 확인하십시오.

사용자 이름과 암호의 유효성을 검사 할 수 있습니까?

유효성을 검사 할 때 다음 코드를 사용하고 있지만 암호를 추가하여 사용자의 유효성을 검사하는 방법을 모르겠습니다.

Public Function FindUserGroupInfo(LoginName As String, GroupName As String) As Boolean 
' Searches for a user within a specified group in Active Directory. 
' Returns TRUE if the user is found in the specified group. 
' Returns FALSE if the user is not found in the group. 

    ' LDAP Search Query Properties 
    Dim conn As New ADODB.Connection ' ADO Connection 
    Dim rs As ADODB.Recordset   ' ADO Recordset 
    Dim oRoot As IADs 
    Dim oDomain As IADs 
    Dim sBase As String 
    Dim sFilter As String 
    Dim sDomain As String 
    Dim sAttribs As String 
    Dim sDepth As String 
    Dim sQuery As String 
    Dim sAns As String 

    ' Search Results 
    Dim user As IADsUser 
    Dim group As Variant 
    Dim usergroup As String 
    Dim userGroupFound As Boolean 

    On Error GoTo ErrHandler: 

    userGroupFound = False 

    'Set root to LDAP/ADO. 
    'LDAP://skb_ii.com/DC=skb_ii,DC=com 
    Set oRoot = GetObject("LDAP://rootDSE") 

    'Create the Default Domain for the LDAP Search Query 
    sDomain = oRoot.Get("defaultNamingContext") 
    Set oDomain = GetObject("LDAP://" & sDomain) 
    sBase = "<" & oDomain.ADsPath & ">" 

    ' Set the LDAP Search Query properties 
    sFilter = "(&(objectCategory=person)(objectClass=user)(name=" & LoginName & "))" 
    sAttribs = "adsPath" 
    sDepth = "subTree" 
    sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth 

    ' Open the ADO connection and execute the LDAP Search query 
    conn.Open "Data Source=Active Directory Provider;Provider=ADsDSOObject" 
    Set rs = conn.Execute(sQuery) ' Store the query results in recordset 

    ' Display the user details 
    If Not rs.EOF Then 
     Set user = GetObject(rs("adsPath")) 

     ' Display the groups memberships 
     For Each group In user.Groups 
      usergroup = group.Name 

      If (InStr(usergroup, GroupName) > 0) Then 
       FindUserGroupInfo = True 
       Exit Function 
      End If 
     Next 
    End If 
    FindUserGroupInfo = userGroupFound 
ErrHandler: 

    On Error Resume Next 
    If Not rs Is Nothing Then 
     If rs.State <> 0 Then rs.Close 
     Set rs = Nothing 
    End If 

    If Not conn Is Nothing Then 
     If conn.State <> 0 Then conn.Close 
     Set conn = Nothing 
    End If 

    Set oRoot = Nothing 
    Set oDomain = Nothing 
End Function 
+0

다른 임의의 언어와 마찬가지로 ge. 적절한 Win32 API 함수를 호출하거나 AD 쿼리를 만듭니다. Technet의 Scriptomatic에서 준비된'vbs' 스크립트를 확인할 수도 있습니다. 당신은 이것들 중 어떤 것을 시도 했습니까? –

+0

감사합니다. 편집 한 질문을 참조하십시오. – Roshe

+0

사실, ** ** 사용자를 확인하려는 이유는 무엇입니까? 로그온 한 도메인 사용자는 AD에 의해 이미 유효성이 검증되었으므로 앱이이를 알고 있으므로 앱에서 사용자의 자격 증명을 사용하려고 시도하는 모든 것이 무엇이든합니다. 다른 도메인 사용자로 가장 하시겠습니까? –

답변

2

사용자를 인증하기 위해 AD 쿼리를 사용할 수 없습니다. 이는 기존 AD 연결에서 executing an LDAP Bind에 의해 수행됩니다. 기본적으로 최종 사용자의 자격 증명으로 연결을 만들어야합니다. 이것이 다양한 .NET 메소드가 내부적으로하는 일입니다.

열기 전에 ADO 연결에 최종 사용자의 자격 증명을 설정하여 COM/VB에서 동일한 기술을 사용할 수 있습니다.

덧붙여 말하자면 현재 코드는 현재 사용자의 자격 증명을 사용하여 쿼리를 실행하려고 시도합니다. 두 도메인간에 트러스트가 있고 원격 도메인이 현재 사용자를 인식하지 않으면이 작업은 실패합니다. 이다

+0

이 코드는 동일한 도메인에서 작동하며 사용자의 사용자 이름을 확인합니다. 내 코드에 맞게 코드를 수정할 수 있습니까? VB 6에 대한 새로운 기능입니까? – Roshe

1

그건 나를 위해 일했다. "이름 ="&에서 LoginName 대신 "SAMAccountName을 = &에서 LoginName"쿼리에, 당신이 시도 할 수 있습니다 "라고. 좀 LDAP 형식의 정보를 웹 사이트에있는 정보를 발견했다.

0

이 솔루션을 찾았습니다. 아래 코드를 사용하여 Active Directory에서 UserID를 쿼리 할 때 Active Directory에서 사용자를 찾을 수없는 경우 쿼리는 "주어진 이름"값을 반환합니다. " do는 반환 값이 ""인지 여부를 확인합니다.

Public Sub TestSub() 
Dim strMyUser As String 

strMyUser = "AB66851" 

If Validation.GetName(strMyUser) <> "" Then 
    MsgBox GetName(strMyUser) 
Else 
    MsgBox strMyUser & " Is not a valid Active Directory ID" 
End If 

End Sub 



Function GetName(strMgrID As String) As String 

Dim objRoot, strDomain, objConn, objComm, objRecordset 
Dim sFilter, sAttribs, sDepth, sBase, sQuery 

Set objRoot = GetObject("LDAP://RootDSE") 
strDomain = objRoot.Get("DefaultNamingContext") 
Set objConn = CreateObject("ADODB.Connection") 
Set objComm = CreateObject("ADODB.Command") 

'sFilter = "(&(objectClass=person)(sn=" & InputBox("Enter Last Name") & ")(givenName=" & InputBox("Enter First Name") & "))" 
sFilter = "(&(objectClass=person)(sAMAccountName=" & strMgrID & "))" 

sAttribs = "sn,givenname,sAMAccountName" 
sDepth = "SubTree" 
sBase = "<LDAP://" & strDomain & ">" 
sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth 

objConn.Open "Data Source=Active Directory Provider;Provider=ADsDSOObject" 
Set objComm.ActiveConnection = objConn 
objComm.Properties("Page Size") = 10000 
objComm.CommandText = sQuery 
Set objRecordset = objComm.Execute 

If Not objRecordset.EOF Then 
    GetName = objRecordset("givenName") & " " & objRecordset("sn") 
End If 
End Function 
관련 문제