Active Directory

rosmiq
10-30-2006, 10:09 AM
Hi,

I am writing an asp.net application using Visual Studio 2003.

I need this webpage to recognise the person loggin in. I have done a User.Identity.Name but need to reference that against the Active Directory. I have this code, which works when run on my own laptop but as soon as I publish this to the webserver it doesn't bring anything back..



#Region "Active Directory"

'This is to connect to the Active Directory
Public Shared Function GetUserInfo(ByVal inSAM As String, ByVal inType As String) As String

Try
Dim sPath As String = "LDAP://sv30/dc=trans,dc=com"
Dim SamAccount As String = Microsoft.VisualBasic.Right(inSAM, Len(inSAM) - InStr(inSAM, "\"))

'This is variable myDirectory as a New DirectoryEntry of the Active Directory
Dim myDirectory As New DirectoryEntry(sPath)

'This is a searcher which searches the myDirectory Variable (i.e. Active Directory)
Dim mySearcher As New DirectorySearcher(myDirectory)

'This is the search result collection to get results together
Dim mySearchResultColl As SearchResultCollection

'This is the search Results
Dim mySearchResult As SearchResult

'These are the properties of the results collection
Dim myResultPropColl As ResultPropertyCollection

'These are the values of the properties of the results collection
Dim myResultPropValueColl As ResultPropertyValueCollection


'Build LDAP Query

mySearcher.Filter = ("(&(objectclass=user)(samaccountname=" & SamAccount & "))")
mySearchResultColl = mySearcher.FindAll()

'I expect only one user from each search result

Select Case mySearchResultColl.Count
Case 0
Return "Null"
Exit Function
Case Is > 1
Return "Null"
Exit Function
End Select

'Get the search result from the collection

mySearchResult = mySearchResultColl.Item(0)

'Get the properties

myResultPropColl = mySearchResult.Properties

'Display name, mail
'Retrieve from the properties collection the display name and email of the user

myResultPropValueColl = myResultPropColl.Item(inType)
Return CStr(myResultPropValueColl.Item(0))

Catch ex As Exception
Console.WriteLine(True)

End Try
End Function




#End Region



Am I doing something wrong?

wayneph
10-30-2006, 10:23 AM
Did you disable the anonymous user in the IIS application on the server?

Also, what error are you getting? Or what is being returned?

rosmiq
10-30-2006, 10:27 AM
Hi Wayneph,

Thank you very much for your reply.

Anonymous Access is enabled on the webserver, should this be disabled?

I'm not getting any errors, its just not returning anything.

shaul_ahuva
10-30-2006, 10:27 AM
You need to use a domain account to access AD - the easiest way I know of is to turn off anonymous authentication (as wayneph suggested) and turn on impersonation (ASP.NET does not normally run as a domain account due to security risks).

You can also provide the user credentials to use when creating the DirectoryEntry object used to access the directory.

rosmiq
10-30-2006, 10:29 AM
I have just disabled Anonymous Access and it worked thank you!!!!!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum