Solved

Vb.Net LDAP Authentication for Windows App

Posted on 2006-11-20
3
4,031 Views
Last Modified: 2009-12-16
Hey All,

Just looking for some ideas in general and perhaps some code too.  I'm developing a windows app (exe) for my company that is going to track hardware upgrades and so on just to give you a general idea of what the application is going to be used for.  I'm going to use VB.net 2k3 for the front end and SQL Server 2k5 for the back end.  

Anyway, rather the create user tables and seperate logins and passwords for everyone that will be using the application I'd like to use LDAP to handle user logins for my application.  So I'm looking for some ideas or some code that will allow me to use LDAP for the User/Client logins for my applications.  Does anyone have any ideas on how I can go about coding my application to use LDAP.  I'm fairly good with Vb.net in general I just haven't had a need to code anything using LDAP until now so I'm not sure where to begin.  Any help that you can provide in the matter would be greatly apprecaited.

Thanks,

Pete J.
0
Comment
Question by:PJan8724
3 Comments
 
LVL 42

Accepted Solution

by:
frodoman earned 500 total points
Comment Utility
Hi Pete - Here's a simple class I used for LDAP authentication on a vb.net web application.  It may be enough to get you started... - frodoman

mports System
Imports System.Text
Imports System.Collections
Imports System.DirectoryServices

Namespace FormsAuth
    Public Class LdapAuthentication

        Dim _path As String
        Dim _filterAttribute As String

        Public Sub New(ByVal path As String)
            _path = path
        End Sub

        Public Function IsAuthenticated(ByVal domain As String, ByVal username As String, ByVal pwd As String) As Boolean

            Dim domainAndUsername As String = domain & "\" & username
            Dim entry As DirectoryEntry = New DirectoryEntry(_path, domainAndUsername, pwd)

            Try
                'Bind to the native AdsObject to force authentication.
                Dim obj As Object = entry.NativeObject
                Dim search As DirectorySearcher = New DirectorySearcher(entry)

                search.Filter = "(SAMAccountName=" & username & ")"
                search.PropertiesToLoad.Add("cn")
                Dim result As SearchResult = search.FindOne()

                If (result Is Nothing) Then
                    Return False
                End If

                'Update the new path to the user in the directory.
                _path = result.Path
                _filterAttribute = CType(result.Properties("cn")(0), String)

            Catch ex As Exception
                Throw New Exception("Error authenticating user. " & ex.Message)
            End Try

            Return True
        End Function

        Public Function GetGroups() As String
            Dim search As DirectorySearcher = New DirectorySearcher(_path)
            search.Filter = "(cn=" & _filterAttribute & ")"
            search.PropertiesToLoad.Add("memberOf")
            Dim groupNames As StringBuilder = New StringBuilder

            Try
                Dim result As SearchResult = search.FindOne()
                Dim propertyCount As Integer = result.Properties("memberOf").Count

                Dim dn As String
                Dim equalsIndex, commaIndex

                Dim propertyCounter As Integer

                For propertyCounter = 0 To propertyCount - 1
                    dn = CType(result.Properties("memberOf")(propertyCounter), String)

                    equalsIndex = dn.IndexOf("=", 1)
                    commaIndex = dn.IndexOf(",", 1)
                    If (equalsIndex = -1) Then
                        Return Nothing
                    End If

                    groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1))
                    groupNames.Append("|")
                Next

            Catch ex As Exception
                Throw New Exception("Error obtaining group names. " & ex.Message)

            End Try

            Return groupNames.ToString()
        End Function
    End Class
End Namespace
0
 
LVL 1

Author Comment

by:PJan8724
Comment Utility
Frodoman,

Thanks for the help, I have to play with it a little bit more before I award the points.  I started playing with it but the code or my application threw an exception error and I need to figure out what caused it.  

Thanks,

Pete J
0
 
LVL 1

Expert Comment

by:ananmanan
Comment Utility
Hi guys,

I tired the same code but it didnt work for me!! so can you send me the part of your code from which you called the isAuthenticatd method? so i can check where i'm making the error. And further i dont understand how the following method work
 Public Sub New(ByVal path As String)
            _path = path
        End Sub

it will be a real help if you can help me out!!

thanks,

ananmanan
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now