Imports System.Text
Imports System.Collections
Imports System.Collections.Generic
Imports System.DirectoryServices
Imports System.DirectoryServices.ActiveDirectory
Imports System.Security
Imports System.Security.Permissions
Public Class Login
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString("ReturnUrl"))
End Sub
Protected Sub LoginButton_Click(sender As Object, e As EventArgs) Handles LoginButton.Click
'Dim usuario As String
'Dim pass As String
Dim valido, valido2 As Boolean
Dim nombre, email, grupo As String
'usuario = Me.UserName.Text
'pass = Me.Password.Text
If Me.UserName.Text = "" Or Me.Password.Text = "" Then
errorLabel.Text = "*Introduzca los datos de acceso necesarios"
Else
GetDirectoryEntry(Me.UserName.Text, Me.Password.Text)
valido = IsValidADLogin(Me.UserName.Text, Me.Password.Text)
If valido Then
valido2 = ValidateUser(Me.UserName.Text, Me.Password.Text)
If valido2 Then
Dim valoresarray As String()
valoresarray = FullName(Me.UserName.Text, Me.Password.Text)
nombre = valoresarray(0)
email = valoresarray(1)
grupo = obtenergrupo(Me.UserName.Text, Me.Password.Text)
'grupo = 2
Session.Add("nombre", nombre)
Session.Add("email", email)
Session.Add("grupoUsuario", grupo)
'Response.Redirect(String.Format("~/PaginaPrincipal.aspx?nombre={0}", nombre))
Response
.Redirect(String.Format("~/PaginaPrincipal.aspx")) 'MsgBox(nombre)
Else
Me.UserName.Text = ""
Me.Password.Text = ""
errorLabel.Text = "*Datos incorrectos. Vuelva a introducir los datos de acceso."
End If
Else
Me.UserName.Text = ""
Me.Password.Text = ""
errorLabel.Text = "*Datos incorrectos. Vuelva a introducir los datos de acceso."
End If
End If
End Sub
Public Shared Function GetDirectoryEntry(ByVal usuario As String, ByVal pass As String) As DirectoryEntry
'Of course change the information for the LDAP to your network
Dim dirEntry As New DirectoryEntry
dirEntry.Path = ("LDAP://192.168.1.108/DC=indar,DC=local")
dirEntry.AuthenticationType = AuthenticationTypes.Secure
dirEntry.Username = "indar.local\" + usuario
dirEntry.Password = pass
Return dirEntry
End Function
Public Shared Function ExtractUserName(ByVal path As String) As String
'Split on the "\"
Dim userPath
As String() = path
.Split(New Char() {"\"c
})
'Return the rest (username part)
Return userPath((userPath.Length - 1))
End Function
Public Function IsValidADLogin(ByVal user As String, ByVal pass As String) As Boolean
Try
'Create a DirectorySearcher Object (used for searching the AD)
Dim search As New DirectorySearcher()
'Set the filter on the searcher object to look for the SAMAccountName, givenName and the sn (Sur Name)
search.Filter = "(sAMAccountName=" + user + ")"
'Use the .FindOne() Method to stop as soon as a match is found
Dim result As SearchResult = search.FindOne()
'Now check to see if a result was found
If result Is Nothing Then
'Login isn't valid
Return False
Else
'Valid login
Return True
End If
Catch ex As Exception
MsgBox("Active Directory Error" & Chr(13) & Chr(13) & ex
.Message) End Try
End Function
Public Function ValidateUser(ByVal user As String, ByVal pass As String) As Boolean
Dim adsEntry As New DirectoryEntry("LDAP://192.168.1.108/DC=indar,DC=local", User, pass)
Dim adsSearcher As New DirectorySearcher(adsEntry)
adsSearcher.Filter = "(sAMAccountName=" + user + ")"
Dim bSucceded As Boolean = False
Try
Dim adsSearchResult As SearchResult
adsSearchResult = adsSearcher.FindOne()
bSucceded = True
Catch ex As Exception
End Try
Return bSucceded
End Function
Public Function FullName(ByVal user As String, ByVal pass As String) As String()
Dim adsEntry As New DirectoryEntry("LDAP://192.168.1.108/DC=indar,DC=local", user, pass)
Dim deSearch As New DirectorySearcher(adsEntry)
Dim properties() As String = {"fullname"}
deSearch.SearchScope = SearchScope.Subtree
deSearch.ReferralChasing = ReferralChasingOption.All
deSearch.PropertiesToLoad.AddRange(properties)
deSearch.Filter = "(sAMAccountName=" + user + ")"
Dim result As SearchResult
result = deSearch.FindOne()
Dim directoryEntry As New DirectoryEntry
directoryEntry = result.GetDirectoryEntry()
Dim displayname, mail As String
displayname = directoryEntry.Properties("displayname").Value
mail = directoryEntry.Properties("mail").Value
Dim ar As String() = {displayname, mail}
Return ar
End Function
Private Function obtenergrupo(ByVal user As String, ByVal pass As String)
Dim deGlobal As New DirectoryEntry("LDAP://192.168.1.108/DC=indar,DC=local", user, pass)
Dim ds As DirectorySearcher = New DirectorySearcher(deGlobal)
ds.SearchScope = DirectoryServices.SearchScope.Subtree
ds.Filter = "(&(objectcategory=user)(SAMAccountName=" & user & "))"
Dim res As SearchResult = ds.FindOne
Dim grupo As String
Dim grupo1, grupo2, grupo3 As Integer
For i = 0 To res.Properties("memberOf").Count() - 1
grupo = res.Properties("memberOf")(i).ToString
If grupo = "CN=GHDGAprobacion,OU=UOHDG,OU=UOAplicaciones,DC=indar,DC=local" Then
grupo1 = 1
ElseIf grupo = "CN=GHDGVisas,OU=UOHDG,OU=UOAplicaciones,DC=indar,DC=local" Then
grupo2 = 2
Else
grupo3 = 3
End If
Next
If grupo1 = 1 Then
If grupo1 = 1 And grupo2 = 2 And grupo3 = 3 Then
Return grupo1
End If
Return grupo1
End If
If grupo2 = 2 Then
If grupo2 = 2 And grupo3 = 3 Then
Return grupo2
End If
Return grupo2
End If
If grupo3 = 3 Then
Return grupo3
End If
End Function
End Class