Wednesday, June 22, 2011

LDAP Authentication with VB.NET

This was kind of tricky for me to figure out. LDAP examples with VB.NET aren't really existent, or, at least they weren't when I put this together. Needed an LDAP authentication library to use with ASPX.

All you have to do with this class is construct it with the FQDN of your LDAP server and call the functions like so:





<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" %>

<%@ Import Namespace="ldapAuth" %>
<%@ Import Namespace="System.DirectoryServices" %>
<%@ Import Namespace="System.DirectoryServices.Protocols" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>LDAP Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%
Dim ldap As New ldapClass("YOURLDAPSERVERFQDN")
Dim username As String = "YOURUSERNAME" 'Username statically declared for a fast example, can be fetched by any method
Dim password As String = "YOURPASSWORD"
Dim ldapConnection As LdapConnection
ldapConnection = ldap.connectSSL(ldap.getUserDN(username), password)
Response.Write("Hostname: " & ldapConnection.SessionOptions.HostName & "<br>")
Response.Write("SSL: " & ldapConnection.SessionOptions.SecureSocketLayer & "<br>")
Response.Write("DN: " & ldap.getUserDN(username))

Using ldapConnection
Dim ldapSearch As SearchRequest = New SearchRequest("dc=YOUR,dc=DOMAIN", "(&(objectClass=YOURUSERFILTER)(UID=" & username & "))", DirectoryServices.Protocols.SearchScope.Subtree)
Dim ldapResponse As SearchResponse = LdapConnection.SendRequest(ldapSearch)

Response.Write("<ul>")
For Each dirEntry As SearchResultEntry In ldapResponse.Entries
For Each attribute As System.Collections.DictionaryEntry In dirEntry.Attributes
Response.Write("<li>" & attribute.Key.ToString & ": " & attribute.Value.item(0).ToString & "</li>")
Next
Next
End Using
%>
</div>
</form>
</body>
</html>





Imports System.DirectoryServices
Imports System.DirectoryServices.Protocols
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates

Public Class ldapClass
Private _ldapServer As String
Dim ldapDirectory As DirectoryEntry
Dim ldapSearcher As DirectorySearcher

Public Property ldapServer()
Get
Return _ldapServer
End Get
Set(ByVal name)
_ldapServer = name
End Set
End Property

Public Sub New(ByVal ldapServerName As String)
ldapServer = ldapServerName
End Sub

Public Function getUserDN(ByVal username As String) As String
Dim ldapConnection As New LdapConnection(ldapServer.ToString)
Dim ldapOptions As LdapSessionOptions = ldapConnection.SessionOptions

ldapOptions.ProtocolVersion = 3
ldapConnection.Credential = New Net.NetworkCredential(String.Empty, String.Empty)
ldapConnection.AuthType = AuthType.Anonymous

ldapConnection.Bind()
Using ldapConnection
Dim ldapSearch As SearchRequest = New SearchRequest("dc=YOURDOMAIN,dc=COM", "(&(objectClass=YOURSEARCHFILTER)(uid=" & username & "))", DirectoryServices.Protocols.SearchScope.Subtree)
Dim ldapResponse As SearchResponse = ldapConnection.SendRequest(ldapSearch)
If ldapResponse.Entries.Count = 0 Then ' No user was found.
Return "Could not find the username requested"
End If
Dim entry As SearchResultEntry = ldapResponse.Entries(0)
Return entry.DistinguishedName
End Using
End Function

Public Function connectSSL(ByVal username As String, ByVal password As String) As LdapConnection
Dim ldapConnection As New LdapConnection(ldapServer.ToString)
Dim ldapOptions As LdapSessionOptions = ldapConnection.SessionOptions

ldapOptions.ProtocolVersion = 3
ldapConnection.SessionOptions.VerifyServerCertificate = New VerifyServerCertificateCallback(AddressOf verifyCallback)
ldapConnection.Credential = New Net.NetworkCredential(username, password)
ldapConnection.SessionOptions.StartTransportLayerSecurity(Nothing)
ldapConnection.AuthType = AuthType.Basic
ldapConnection.Bind()
Return ldapConnection
End Function


Public Function verifyCallback(ByVal connection As LdapConnection, ByVal certificate As X509Certificate) As Boolean
'Do some stuff here to verify the server certificate, if you choose.
Return True
End Function


End Class

No comments:

Post a Comment