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
Solved

Vb.Net LDAP Authentication for Windows App

Posted on 2006-11-20
3
4,038 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 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