0 Replies - 564 Views - Last Post: 06 February 2018 - 04:55 AM Rate Topic: -----

#1 JessNutt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-February 18

vb - Authenticate User against Active Directory

Posted 06 February 2018 - 04:55 AM

I have been asked to create a login form in Visual Studio using Windows Forms in VB, which has a forgot password button which when selected will check the user against active directory and it finds it will allow the user to change their password.

However I only know basic VB as I have not used it for very long and I have got no idea where to begin. I have searched on google for articles on how to do it and found the code below however I do not understand what the code does or how I would incorporate it, could anyone help explain this and how I could use it?

    Private Function ValidateActiveDirectoryLogin(ByVal Domain As String, ByVal Username As String, ByVal Password As String) As Boolean
            Dim Success As Boolean = False
            Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://" & Domain, Username, Password)
            Dim Searcher As New System.DirectoryServices.DirectorySearcher(Entry)
            Searcher.SearchScope = DirectoryServices.SearchScope.OneLevel
            'Try
            '    Dim Results As System.DirectoryServices.SearchResult = Searcher.FindOne
            '    Success = Not (Results Is Nothing)
            'Catch
            '    Success = False
            'End Try
     
            Dim Results As System.DirectoryServices.SearchResult = Searcher.FindOne
            Success = Not (Results Is Nothing)
            Return Success
        End Function
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
   Dim authenticated As Boolean = AuthenticateUser()
   If isAuthenticated Then
      'some code here to open app
      Me.Close()
   Else
      'code here to display incorrect login details
   End If
End Sub

Private Function AuthenticateUser() As Boolean
   Dim username As String = txtUsername.Text
   Dim password As String = txtPassword.Text
   Dim domain As String = 'this can be in a config file, hard coded (I wouldnt do that), or inputed from the UI

   Dim isAuthenticated As Boolean = ValidateActiveDirectoryLogin(username, password, domain)

   Return isAuthenticated
End Function



Is This A Good Question/Topic? 0
  • +

Page 1 of 1