473,503 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Active Directory lookup

I want to do an AD lookup of the current logged on user. From ASP.NET
I can get the identity name of the user in the form of
"<domain>\<user>". How do I perform an AD search based on that
information using the syntax "LDAP://"?

Nov 19 '05 #1
4 3786
Something like the code example below should work. Some keys things to
mention:
1) ASP.NET does not have access to domains Active Directory. You need to
create a domain user, with minimal permissions, which the app can then use
to perform ldap queries: this would be used to set the ldapUser and
ldapPassword noted in code below.
2) You will obviously need to store the username and password noted above so
they should be encrypted to keep someone from misusing the account.
3) You need to know the name of your AD domain to format the ldapPath
properly. If your domain was "mycompany.net", the format of the ldapPath
would be something like "LDAP://DC=mycompany,DC=net". See the Active
Directory docs or at least the doc for framework classes such as
DirectoryEntry for more info.

Hope this helps.
Imports System.Security.Principal
Imports System.DirectoryServices

Friend Class MySearcher

Public Sub DoSearch
' Need to get or set ldapPath, ldapUser and ldapPassword

Dim de As DirectoryEntry = New DirectoryEntry(ldapPath, ldapUser,
ldapPassword)
Dim searcher As DirectorySearcher = New DirectorySearcher(de)

' Parse domain\user to get just the user
Dim domainUserName As String = WindowsIdentity.GetCurrent.Name
Dim myID As String = myID.Substring(domainUserName.IndexOf("\") + 1)

searcher.Filter = String.Format("(SAMAccountName={0})",myID)"
searcher.FindOne()
Dim sr As SearchResult = searcher.FindOne

If Not (sr Is Nothing) Then

' "my" objects below would be something like class properties

myUserName = GetAdProperty(sr, "SAMAccountName")
myFirstName = GetAdProperty(sr, "givenname")
myMiddleName = GetAdProperty(sr, "middlename")
myLastName = GetAdProperty(sr, "sn")
myCompany = GetAdProperty(sr, "company")
myDepartment = GetAdProperty(sr, "department")
myTitle = GetAdProperty(sr, "title")
myEmail = GetAdProperty(sr, "mail")
myPhone = GetAdProperty(sr, "telephonenumber")

' more properties are available....just some examples of
typical ones

End If
End Sub

'**************************************
' Helper function to get the value in the AD property
Private Function GetAdProperty(ByVal sr As SearchResult, ByVal
propertyName As String) As String
Try
Return sr.Properties(propertyName)(0).ToString
Catch ex As Exception
Return ""
End Try
End Function

End Class
<pt******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
I want to do an AD lookup of the current logged on user. From ASP.NET
I can get the identity name of the user in the form of
"<domain>\<user>". How do I perform an AD search based on that
information using the syntax "LDAP://"?

Nov 19 '05 #2
Something like the code example below should work. Some keys things to
mention:
1) ASP.NET does not have access to domains Active Directory. You need to
create a domain user, with minimal permissions, which the app can then use
to perform ldap queries: this would be used to set the ldapUser and
ldapPassword noted in code below.
2) You will obviously need to store the username and password noted above so
they should be encrypted to keep someone from misusing the account.
3) You need to know the name of your AD domain to format the ldapPath
properly. If your domain was "mycompany.net", the format of the ldapPath
would be something like "LDAP://DC=mycompany,DC=net". See the Active
Directory docs or at least the doc for framework classes such as
DirectoryEntry for more info.

Hope this helps.
Imports System.Security.Principal
Imports System.DirectoryServices

Friend Class MySearcher

Public Sub DoSearch
' Need to get or set ldapPath, ldapUser and ldapPassword

Dim de As DirectoryEntry = New DirectoryEntry(ldapPath, ldapUser,
ldapPassword)
Dim searcher As DirectorySearcher = New DirectorySearcher(de)

' Parse domain\user to get just the user
Dim domainUserName As String = WindowsIdentity.GetCurrent.Name
Dim myID As String = myID.Substring(domainUserName.IndexOf("\") + 1)

searcher.Filter = String.Format("(SAMAccountName={0})",myID)"
searcher.FindOne()
Dim sr As SearchResult = searcher.FindOne

If Not (sr Is Nothing) Then

' "my" objects below would be something like class properties

myUserName = GetAdProperty(sr, "SAMAccountName")
myFirstName = GetAdProperty(sr, "givenname")
myMiddleName = GetAdProperty(sr, "middlename")
myLastName = GetAdProperty(sr, "sn")
myCompany = GetAdProperty(sr, "company")
myDepartment = GetAdProperty(sr, "department")
myTitle = GetAdProperty(sr, "title")
myEmail = GetAdProperty(sr, "mail")
myPhone = GetAdProperty(sr, "telephonenumber")

' more properties are available....just some examples of
typical ones

End If
End Sub

'**************************************
' Helper function to get the value in the AD property
Private Function GetAdProperty(ByVal sr As SearchResult, ByVal
propertyName As String) As String
Try
Return sr.Properties(propertyName)(0).ToString
Catch ex As Exception
Return ""
End Try
End Function

End Class
<pt******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
I want to do an AD lookup of the current logged on user. From ASP.NET
I can get the identity name of the user in the form of
"<domain>\<user>". How do I perform an AD search based on that
information using the syntax "LDAP://"?

Nov 19 '05 #3
Brad,

What happens when you have two different users in two different
domains with the same sAMAccount. For example domain1\user and
domain2\user. I can't use the sAMAccount because AD doesn't know which
one to choose. Is there a way to query AD without having me to code an
if-else statement for each domain lookup.

Nov 19 '05 #4
I don't know....I haven't had to do that. I suppose one option would be to
set the value of the LDAP path according to the domain of the user (which
you would parse out of the windowsidentity). That way you would still be
using the samaccount and only be searching the domain which is applicable to
the specific domain\user.

<pt******@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Brad,

What happens when you have two different users in two different
domains with the same sAMAccount. For example domain1\user and
domain2\user. I can't use the sAMAccount because AD doesn't know which
one to choose. Is there a way to query AD without having me to code an
if-else statement for each domain lookup.

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
634
by: Henning Kristensen | last post by:
Hi I need a little help with some VB.Net code.. I am pulling my computer's GUID out using WMI Dim wmiObj As New System.Management.ManagementObjectSearcher("SELECT UUID FROM...
0
352
by: Henning Kristensen | last post by:
Hi I need a little help with some VB.Net code.. I am pulling my computer's GUID out using WMI Dim wmiObj As New System.Management.ManagementObjectSearcher("SELECT UUID FROM...
5
3844
by: David W. Fenton | last post by:
Does anyone know of any Windows API calls to get to Active Directory organizational unit information? I searched for this a while back and found nothing. It would be a great thing if I could get to...
0
1114
by: Andrew Connell | last post by:
I have a case where I need to be able to query Active Directory to look up a user by their WINNT:\\DOMAIN\USERACCOUNT. The site is authenticating the users (it's a CMS site and they must be...
2
1150
by: Eman | last post by:
Hi, I'm new with .net technology, currently I'm developing an application by using ASP .net, I want to link it with the Active Directory on the Domain Controller. I know that I should add...
0
975
by: Gary | last post by:
I'm creating an Infopath form and I need to query Active Directory to obtain some data. Here is the code I'm using (obtained from sample code from MSDN) Private Shared domainADsPath As String =...
2
9255
by: Max Vit | last post by:
I have been toying with the idea of having an Active Directory authentication / authorisation functionality in MS Access; but there is not much useful info around. I have built these...
0
743
by: mankz | last post by:
Hi! I wonder if it's possible to send emails to active directory users without fully specifying their email addresses. In Outlook for example I can just type the recipient as "someuserid" which is...
0
2394
by: Zetten | last post by:
I'm trying to develop a page which will grab a few details for the currently logged-in Windows user from the Active Directory controller. I can get the full name of the user from their username...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.