Public Sub GetADSILogin()
Try
Dim strServerName As String = ""
Dim strBaseDN As String = ""
Dim strUserDN As String = ""
Dim strGroupName As String = ""
Dim strAccountFilter As String = ""
'Port no for LDAP Default is 389
Dim strPortNo As String = "389"
Dim blnGroupUser As [Boolean] = False
'Data source string
Dim [source] As String = "Data Source=ATHAKUR;Initial Catalog=Times;user=sa;password=sa"
'SQL statement that will be issued
Dim [select] As String = "SELECT * from ADSI_PARAMETER"
'SQL Connection
Dim conn As New SqlConnection([source])
' Open the database connection
conn.Open()
' Create the SQL command...
Dim cmd As New SqlCommand([select], conn)
'Execute Data reader
Dim myReader As SqlDataReader = cmd.ExecuteReader()
'Check if any rows return against user/pass
If myReader.HasRows Then
While myReader.Read()
'Store the parameter's data in variables
Dim strParameterName As String = myReader.GetString(0).Trim()
Dim strParameterValue As String = myReader.GetString(1).Trim()
If strParameterName.ToUpper().Equals("SERVERNAME") Then
strServerName = strParameterValue
End If
If strParameterName.ToUpper().Equals("BASEDN") Then
strBaseDN = strParameterValue
End If
If strParameterName.ToUpper().Equals("USERDN") Then
strUserDN = strParameterValue
End If
If strParameterName.ToUpper().Equals("GROUPNAME") Then
strGroupName = strParameterValue
End If
If strParameterName.ToUpper().Equals("ACCOUNTFILTER") Then
strAccountFilter = strParameterValue
End If
End While
End If 'Search for user
Dim deSystem As New DirectoryEntry("LDAP://" + strServerName + "/" + strUserDN + "," + strBaseDN)
deSystem.AuthenticationType = AuthenticationTypes.Secure
deSystem.Username = txtUserName.Text
deSystem.Password = txtPassword.Text
'Search for account name
Dim strSearch As String = strAccountFilter + "=" + txtUserName.Text
Dim dsSystem As New DirectorySearcher(deSystem, strSearch)
'Search subtree of UserDN
dsSystem.SearchScope = SearchScope.Subtree
'Find the user data
Dim srSystem As SearchResult = dsSystem.FindOne()
'Pick up the user group belong to
Dim valcol As ResultPropertyValueCollection = srSystem.Properties("memberOf")
If valcol.Count > 0 Then
Dim o As Object
For Each o In valcol
'check user exist in Group we are searching for
If o.ToString().Equals((strGroupName + "," + strBaseDN)) Then
blnGroupUser = True
Exit ForEach
End If
Next o
End If
If blnGroupUser = True Then
MessageBox.Show("Login Sucessfull...")
Else
MessageBox.Show("User Does Not Belong to Specified ADSI Group")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
i = i + 1
If i = 5 Then
MessageBox.Show("Login failed for 5 times. Quiting...")
Me.Close()
End If
End Sub 'GetADSILogin