?
Solved

Vb.Net LDAP Authentication for Windows App

Posted on 2006-11-20
3
Medium Priority
?
4,043 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 42

Accepted Solution

by:
frodoman earned 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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