martes, diciembre 04, 2012

Codigo Autenticacion contra active directory VB.NET

Como estoy desarrollando sistemas me encontré con este requerimiento y busque en internet como autenticar mi aplicación ya sea vb.net o asp.net contra un servidor de dominio active directory. Aquí dejo la función y los espacios de nombres a referenciar.


Imports System.DirectoryServices.AccountManagement
Imports System.DirectoryServices


Public Function IsAuthenticated(ByVal Domain As String, ByVal username As String, ByVal pwd As String) As Boolean
        Dim Success As Boolean = False
        Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://" & Domain, username, pwd)
        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
        Return Success
    End Function