Solved

Vb.Net LDAP Authentication for Windows App

Posted on 2006-11-20
3
4,037 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
ID: 17986815
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
ID: 18011575
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
ID: 21263436
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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