Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
rafaelsantos

Dúvida: AD retirar as informações de um User do servidor

Recommended Posts

rafaelsantos

Boas,

recentemente o meu professor pediu-me para fazer um programinha, tinha de fazer um pequeno form inicial de login, mas o login era feito com users da escola. Acontece que fiquei baralhado e ele disse-me que tinha de mexer com Active Directory e DirectoryEntry, DirectorySearcher e assim. O meu problema é retirar a informação do servidor da escola para poder confirmar o Nome do User e a sua Password para assim deixa-lo entrar na aplicação.

Deixo aqui o código que já fiz:

Public Class Form1
    Public Shared Function GetDirectoryEntry() As DirectoryServices.DirectoryEntry
        Dim de As DirectoryServices.DirectoryEntry = New DirectoryServices.DirectoryEntry()
        de.Path = "LDAP://10.0.0.1/CN=Users;DC=alunos"
        de.AuthenticationType = DirectoryServices.AuthenticationTypes.Secure
        Return de
    End Function
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BackColor = Color.DarkGray
        TransparencyKey = BackColor
        GetDirectoryEntry()
    End Sub
    Private Sub btnentrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnentrar.Click
        Dim username As String = txtuser.Text
        UserExists(username)
    End Sub
    Public Function UserExists(ByVal username As String) As Boolean
        Dim de As DirectoryServices.DirectoryEntry = GetDirectoryEntry()
        Dim deSearch As DirectoryServices.DirectorySearcher = New DirectoryServices.DirectorySearcher()
        deSearch.SearchRoot = de
        deSearch.Filter = "(&(objectClass=user)(cn=" & username & "))"
        Dim results As DirectoryServices.SearchResultCollection = deSearch.FindAll()
        If results.Count = 0 Then
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
        Close()
    End Sub

    Private Sub Label3_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label3.MouseHover
        Label3.ForeColor = Color.Blue
    End Sub

    Private Sub Label3_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label3.MouseLeave
        Label3.ForeColor = Color.Black
    End Sub
End Class

Desde já agradeço.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.