J
Jon Delano
Hello
After some effort I was able to come up with this class (converted from a C#
example on MS' web site).
Put this in a class in you vb.net web project and you should be able to
authenticate a user against active directory and also retrieve their user
groups.
You must replace the LDAP://yourdomain with the actual domain that you'd
like to authenticate against.
Please do with this as you will (and use at your own risk):
Imports System.DirectoryServices
Imports System.Runtime.InteropServices
Imports System.Globalization
Public Class ADAuthenticate
Private _path As String
Private _filterAttribute As String
Private _UserFirstName As String
Private _UserLastName As String
Private _UserPath As String
Private _AuthenticationErrorString As String
Public Function IsAuthenticated(ByVal UserName As String, ByVal Password
As String) As Boolean
' authenticate the user and get some info about them
Dim entry As New DirectoryEntry("LDAP://yourdomain", UserName,
Password, System.DirectoryServices.AuthenticationTypes.Secure)
Dim ds As New DirectorySearcher(entry)
Dim myFilter As String = "(&(objectClass=user)(samaccountname=" +
UserName + "))"
ds.Filter = myFilter
ds.PropertiesToLoad.Add("sn")
ds.PropertiesToLoad.Add("GivenName")
Try
Dim sRslt As SearchResult = ds.FindOne
If sRslt Is Nothing Then
Return False
Else
_filterAttribute = UserName
Dim propName As String
Dim value As Object
For Each propName In sRslt.Properties.PropertyNames
For Each value In sRslt.Properties(propName)
If propName = "sn" Then
_UserLastName = value
End If
If propName = "givenname" Then
_UserFirstName = value
End If
If propName = "adspath" Then
_UserPath = value
End If
Next value
Next propName
Return True
End If
sRslt = Nothing
ds = Nothing
Catch ex As Exception
_AuthenticationErrorString = ex.Message & "<br>" & ex.StackTrace
Return False
Finally
entry.Dispose()
entry = Nothing
ds.Dispose()
ds = Nothing
End Try
End Function
Public ReadOnly Property LdapAuthenticationErrorString() As String
Get
Return _AuthenticationErrorString
End Get
End Property
Public ReadOnly Property LdapUserFirstName() As String
Get
Return _UserFirstName
End Get
End Property
Public ReadOnly Property LdapUserLastName() As String
Get
Return _UserLastName
End Get
End Property
Public Function GetUserGroups(ByVal UserName As String, ByVal Password
As String) As String
' get the groups the user belongs to
Dim entry As New DirectoryEntry("LDAP://yourdomain", UserName,
Password, System.DirectoryServices.AuthenticationTypes.Secure)
Dim search = New DirectorySearcher(entry)
search.Filter = "(&(objectClass=user)(cn=" + _filterAttribute + "))"
search.PropertiesToLoad.Add("memberOf")
Dim groupNames As New System.Text.StringBuilder()
Try
Dim result As SearchResult = search.FindOne()
Dim propertyCount As Int32 = result.Properties("memberOf").Count
Dim dn As String
Dim equalsIndex As Int32, commaIndex As Int32
Dim propertyCounter As Int32
For propertyCounter = 0 To propertyCount - 1
dn = result.Properties("memberOf")(propertyCounter)
equalsIndex = dn.IndexOf("=", 1)
commaIndex = dn.IndexOf(",", 1)
If (-1 = equalsIndex) Then
groupNames.Append(dn)
Else
groupNames.Append(dn.Substring((equalsIndex + 1),
(commaIndex - equalsIndex) - 1))
groupNames.Append("|")
End If
Next propertyCounter
Catch ex As Exception
Throw New Exception("Error obtaining group names. " +
ex.Message)
Finally
entry.Dispose()
entry = Nothing
search = Nothing
End Try
Return groupNames.ToString()
End Function
End Class
Good luck
Jon
After some effort I was able to come up with this class (converted from a C#
example on MS' web site).
Put this in a class in you vb.net web project and you should be able to
authenticate a user against active directory and also retrieve their user
groups.
You must replace the LDAP://yourdomain with the actual domain that you'd
like to authenticate against.
Please do with this as you will (and use at your own risk):
Imports System.DirectoryServices
Imports System.Runtime.InteropServices
Imports System.Globalization
Public Class ADAuthenticate
Private _path As String
Private _filterAttribute As String
Private _UserFirstName As String
Private _UserLastName As String
Private _UserPath As String
Private _AuthenticationErrorString As String
Public Function IsAuthenticated(ByVal UserName As String, ByVal Password
As String) As Boolean
' authenticate the user and get some info about them
Dim entry As New DirectoryEntry("LDAP://yourdomain", UserName,
Password, System.DirectoryServices.AuthenticationTypes.Secure)
Dim ds As New DirectorySearcher(entry)
Dim myFilter As String = "(&(objectClass=user)(samaccountname=" +
UserName + "))"
ds.Filter = myFilter
ds.PropertiesToLoad.Add("sn")
ds.PropertiesToLoad.Add("GivenName")
Try
Dim sRslt As SearchResult = ds.FindOne
If sRslt Is Nothing Then
Return False
Else
_filterAttribute = UserName
Dim propName As String
Dim value As Object
For Each propName In sRslt.Properties.PropertyNames
For Each value In sRslt.Properties(propName)
If propName = "sn" Then
_UserLastName = value
End If
If propName = "givenname" Then
_UserFirstName = value
End If
If propName = "adspath" Then
_UserPath = value
End If
Next value
Next propName
Return True
End If
sRslt = Nothing
ds = Nothing
Catch ex As Exception
_AuthenticationErrorString = ex.Message & "<br>" & ex.StackTrace
Return False
Finally
entry.Dispose()
entry = Nothing
ds.Dispose()
ds = Nothing
End Try
End Function
Public ReadOnly Property LdapAuthenticationErrorString() As String
Get
Return _AuthenticationErrorString
End Get
End Property
Public ReadOnly Property LdapUserFirstName() As String
Get
Return _UserFirstName
End Get
End Property
Public ReadOnly Property LdapUserLastName() As String
Get
Return _UserLastName
End Get
End Property
Public Function GetUserGroups(ByVal UserName As String, ByVal Password
As String) As String
' get the groups the user belongs to
Dim entry As New DirectoryEntry("LDAP://yourdomain", UserName,
Password, System.DirectoryServices.AuthenticationTypes.Secure)
Dim search = New DirectorySearcher(entry)
search.Filter = "(&(objectClass=user)(cn=" + _filterAttribute + "))"
search.PropertiesToLoad.Add("memberOf")
Dim groupNames As New System.Text.StringBuilder()
Try
Dim result As SearchResult = search.FindOne()
Dim propertyCount As Int32 = result.Properties("memberOf").Count
Dim dn As String
Dim equalsIndex As Int32, commaIndex As Int32
Dim propertyCounter As Int32
For propertyCounter = 0 To propertyCount - 1
dn = result.Properties("memberOf")(propertyCounter)
equalsIndex = dn.IndexOf("=", 1)
commaIndex = dn.IndexOf(",", 1)
If (-1 = equalsIndex) Then
groupNames.Append(dn)
Else
groupNames.Append(dn.Substring((equalsIndex + 1),
(commaIndex - equalsIndex) - 1))
groupNames.Append("|")
End If
Next propertyCounter
Catch ex As Exception
Throw New Exception("Error obtaining group names. " +
ex.Message)
Finally
entry.Dispose()
entry = Nothing
search = Nothing
End Try
Return groupNames.ToString()
End Function
End Class
Good luck
Jon