Ver Mensaje Individual
  #4 (permalink)  
Antiguo 07/01/2013, 09:35
maialenlopez
 
Fecha de Ingreso: abril-2012
Mensajes: 449
Antigüedad: 12 años, 1 mes
Puntos: 7
Respuesta: Obtener desde directorio activo nombre y apellido de empleado e email

Hola;

He conseguido que medio funcione, pero necesito que en la funcion ValidateUser, que mostraré en el código de abajo, no solo me valide con el user si no que me valide con el user y con el pass. ¿Como puedo hacerlo?

Código vb.net:
Ver original
  1. Imports System.Text
  2. Imports System.Collections
  3. Imports System.Collections.Generic
  4. Imports System.DirectoryServices
  5. Imports System.DirectoryServices.ActiveDirectory
  6. Imports System.Security
  7. Imports System.Security.Permissions
  8.  
  9. Public Class Login
  10.     Inherits System.Web.UI.Page
  11.  
  12.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  13.         'RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString("ReturnUrl"))
  14.     End Sub
  15.  
  16.     'Public _Path As String = "LDAP://192.168.1.108/DC=indar,DC=local"
  17.  
  18.     Protected Sub LoginButton_Click(sender As Object, e As EventArgs) Handles LoginButton.Click
  19.         Dim usuario As String
  20.         Dim pass As String
  21.         Dim valido As Boolean
  22.         Dim nombre As String
  23.  
  24.         usuario = Me.UserName.Text
  25.         pass = Me.Password.Text
  26.  
  27.         If usuario = "" Or pass = "" Then
  28.  
  29.             errorLabel.Text = "*Introduzca los datos de acceso necesarios"
  30.         Else
  31.             GetDirectoryEntry(usuario, pass)
  32.             valido = IsValidADLogin(usuario, pass)
  33.  
  34.             If valido Then
  35.  
  36.                 [B]ValidateUser[/B](usuario, pass)
  37.                 nombre = FullName(usuario, pass)
  38.             Else
  39.                 Me.UserName.Text = ""
  40.                 Me.Password.Text = ""
  41.                 errorLabel.Text = "*Datos incorrectos. Vuelva a introducir los datos de acceso."
  42.             End If
  43.         End If
  44.     End Sub
  45.  
  46.     Public Shared Function GetDirectoryEntry(ByVal usuario As String, ByVal pass As String) As DirectoryEntry
  47.         'Of course change the information for the LDAP to your network
  48.         Dim dirEntry As New DirectoryEntry
  49.  
  50.         dirEntry.Path = ("LDAP://192.168.1.108/DC=indar,DC=local")
  51.         dirEntry.AuthenticationType = AuthenticationTypes.Secure
  52.         dirEntry.Username = usuario
  53.         dirEntry.Password = pass
  54.  
  55.         Return dirEntry
  56.     End Function
  57.  
  58.     Public Shared Function ExtractUserName(ByVal path As String) As String
  59.         'Split on the "\"
  60.         Dim userPath As String() = path.Split(New Char() {"\"c})
  61.  
  62.         'Return the rest (username part)
  63.         Return userPath((userPath.Length - 1))
  64.     End Function
  65.  
  66.     Public Function IsValidADLogin(ByVal user As String, ByVal pass As String) As Boolean
  67.  
  68.         Try
  69.             'Create a DirectorySearcher Object (used for searching the AD)
  70.             Dim search As New DirectorySearcher()
  71.  
  72.             'Set the filter on the searcher object to look for the SAMAccountName, givenName and the sn (Sur Name)
  73.             search.Filter = "(sAMAccountName=" + user + ")"
  74.  
  75.             'Use the .FindOne() Method to stop as soon as a match is found
  76.             Dim result As SearchResult = search.FindOne()
  77.  
  78.             'Now check to see if a result was found
  79.             If result Is Nothing Then
  80.  
  81.                 'Login isn't valid
  82.                 Return False
  83.             Else
  84.  
  85.                 'Valid login
  86.                 Return True
  87.             End If
  88.         Catch ex As Exception
  89.             MsgBox("Active Directory Error" & Chr(13) & Chr(13) & ex.Message)
  90.         End Try
  91.     End Function
  92.  
  93.     Public Function [B]ValidateUser[/B](ByVal user As String, ByVal pass As String) As Boolean
  94.         Dim adsEntry As New DirectoryEntry("LDAP://192.168.1.108/DC=indar,DC=local", user, pass)
  95.         Dim adsSearcher As New DirectorySearcher(adsEntry)
  96.  
  97.         [COLOR="Red"]adsSearcher.Filter = "sAMAccountName=" + user + ")"[/COLOR]
  98.         Dim bSucceded As Boolean = False
  99.  
  100.         Try
  101.             Dim adsSearchResult As SearchResult
  102.  
  103.             adsSearchResult = adsSearcher.FindOne()
  104.             bSucceded = True
  105.             adsEntry.Close()
  106.         Catch ex As Exception
  107.  
  108.             Dim strError As String = ex.Message
  109.             adsEntry.Close()
  110.         End Try
  111.         Return bSucceded
  112.     End Function
  113.  
  114.     Public Function FullName(ByVal user As String, ByVal pass As String) As String
  115.         Dim adsEntry As New DirectoryEntry("LDAP://192.168.1.108/DC=indar,DC=local", user, pass)
  116.         Dim deSearch As New DirectorySearcher(adsEntry)
  117.         Dim properties() As String = {"fullname"}
  118.         'string[] properties = new string[] { "fullname" };
  119.         deSearch.SearchScope = SearchScope.Subtree
  120.         deSearch.ReferralChasing = ReferralChasingOption.All
  121.         deSearch.PropertiesToLoad.AddRange(properties)
  122.         deSearch.Filter = "sAMAccountName=" + user + ")"
  123.  
  124.         Dim result As SearchResult
  125.         result = deSearch.FindOne()
  126.         Dim directoryEntry As New DirectoryEntry
  127.         directoryEntry = result.GetDirectoryEntry()
  128.         Dim displayname As String
  129.         displayname = directoryEntry.Properties("displayname")(0).ToString()
  130.         Return displayname
  131.     End Function
  132. End Class
__________________
Gracias por todo;

Un saludo