473,413 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Using ASP.Net to Get User Information from AD

Hey list, got this code running in a webform:

Dim DSESearcher As System.DirectoryServices.DirectorySearcher = New
System.DirectoryServices.DirectorySearcher
Dim RootDSE As String = DSESearcher.SearchRoot.Path
RootDSE = RootDSE.Insert(7, "ou=users,ou=myusers,")
Dim myDE As DirectoryEntry = New DirectoryEntry(RootDSE)
Dim myEntries As DirectoryEntries = myDE.Children
Dim thisentry As DirectoryEntry
thisentry = myEntries.Find("cn=" & lastname)
Label1.Text = thisentry.Name

Obviously, this won't work because it's running as the ASP.Net user. How do
I go about getting another or hardcoded user to authenticate with
DSESearcher?

Thanks!
Derek
Nov 21 '05 #1
1 5011
See
http://support.microsoft.com/default...b;en-us;329986

Imports System
Imports System.Diagnostics
Imports System.DirectoryServices
Imports System.Xml

Namespace ASPNet_App
'/ <summary>
'/
'/ </summary>
Public Class ADSIUtil
Protected m_strErrors As String = ""
Protected m_strRoot As String = ""
Protected m_obDirEnTry As DirectoryEnTry
Public Sub New()

End Sub
Public ReadOnly Property Errors() As String
Get
Return m_strErrors
End Get
End Property
Public Property RootNode() As String
Get
Return m_strRoot
End Get
Set (ByVal Value As String)
m_strRoot = value
End Set
End Property
Public Function Initialize(ByVal strRoot As String) As Boolean
If m_strRoot = Nothing Or m_strRoot.Length = 0 Then
RootNode = strRoot
End If

Try
m_obDirEnTry = New DirectoryEnTry(strRoot)
'--*' foreach (String propName in
m_obDirEnTry.Properties.PropertyNames)
' {
' foreach (Object value in m_obDirEnTry.Properties(propName))
' {
' Trace.WriteLine("name=" + propName + " value=" + value)
' }
' }
' */
Catch ex As Exception
Trace.WriteLine(ex.Message)
Return False
End Try
Return True
End Function
Public Function GetUserSchema(ByVal strLogin As String) As XmlDocument
Dim userDataDoc As XmlDocument = Nothing
' Make sure that we have a root node specified.
If m_strRoot = Nothing Or m_strRoot.Length = 0 Then
m_strErrors += "Root Node not initializes"
Return userDataDoc
End If

Dim results As SearchResultCollection
Dim srch As DirectorySearcher = New DirectorySearcher(m_obDirEnTry)
srch.Filter = ("(cn=" + strLogin + ")")

Try
results = srch.FindAll()
Catch ex As NotSupportedException
m_strErrors += ex.Message
Trace.WriteLine(ex.Message)
Return userDataDoc
Catch ex As Exception
m_strErrors += "\n"
m_strErrors += ex.Message
Trace.WriteLine(ex.Message)
Return Nothing
End Try

Try
Dim result As SearchResult
For Each result In results
Dim coll As ResultPropertyCollection = result.Properties
Dim user As ADSIUser = New ADSIUser()
If False = user.Initialize(coll) Then
m_strErrors += "\n"
m_strErrors += "Failed to initialize the ADSI object"
Trace.WriteLine("Failed to initialize the ADSI object")
Return Nothing
End If

If user.NumProperties > 0 Then
userDataDoc = user.GetUserProperties()
End If
Next
Catch ex As Exception
m_strErrors += "\n"
m_strErrors += ex.Message
Trace.WriteLine(ex.Message)
Return Nothing
End Try

Return userDataDoc
End Function

'/ <summary>
'/
'/ </summary>
'/ <param name="strUser"></param>
'/ <returns></returns>
Private Function DumpSchema(ByVal strUser As String) As Boolean
If m_strRoot.Length = 0 Then
Return False
End If

Dim results As SearchResultCollection
Dim srch As DirectorySearcher = Nothing
Try
'DirectoryEntry schemaEntry = m_obDirEntry.SchemaEntry;
'Trace.WriteLine(schemaEntry.Children.ToString());
srch = New DirectorySearcher(m_obDirEnTry)
srch.Filter = ("(cn=" + strUser + ")")
results = srch.FindAll()

Dim result As SearchResult
For Each result In results
Dim coll As ResultPropertyCollection = result.Properties
Dim nItems As Integer = coll.Count
Dim myKey As String
For Each myKey In coll.PropertyNames
Dim tab As String = " "
Trace.WriteLine(myKey + " = ")
Dim myCollection As Object
For Each myCollection In coll(myKey)
Trace.WriteLine(tab + myCollection)
Next
Next
Next

Catch ex As Exception
Trace.WriteLine(ex.Message)
End Try

Return True
End Function
End Class
End Namespace

"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:dm*****@DONTSPAMMEokstateDOT.edu:
Hey list, got this code running in a webform:

Dim DSESearcher As System.DirectoryServices.DirectorySearcher = New
System.DirectoryServices.DirectorySearcher
Dim RootDSE As String = DSESearcher.SearchRoot.Path
RootDSE = RootDSE.Insert(7, "ou=users,ou=myusers,")
Dim myDE As DirectoryEntry = New DirectoryEntry(RootDSE)
Dim myEntries As DirectoryEntries = myDE.Children
Dim thisentry As DirectoryEntry
thisentry = myEntries.Find("cn=" & lastname)
Label1.Text = thisentry.Name

Obviously, this won't work because it's running as the ASP.Net user. How
do
I go about getting another or hardcoded user to authenticate with
DSESearcher?

Thanks!
Derek


Nov 21 '05 #2

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

Similar topics

0
by: Joe | last post by:
All While I know how to get the MS networking user information, I am looking for how to get th Novell login information as well. I should be able to get this information as easily as the M...
2
by: DotNetJunkies User | last post by:
Does anyone know how to get the user information such as Name, Address, Email, Phone Number, etc from Windows Active Directory? The samples I have seen so far don’t get the User Address, Email, or...
1
by: Haffi | last post by:
hi, I have a proplem creating a new user and/or adding additional information for root user in MySQL Administrator 1.0.19. When I do I get this message: error while storing the user information....
0
by: Federica | last post by:
Hi! I need some help retrieving user information in Active Directory. I have one webform with 2 textboxes 1 username and one for password. When the user load this page and insert his username and...
4
by: sck10 | last post by:
Hello, I built a web site on our companies intranet. Is there a way to get the user information (user name, machine name etc.) when a person views a web page? -- Thanks in advance, sck10
7
by: msc | last post by:
Hi guys, How to get the user information like userid written in C language.
3
by: BlueSky | last post by:
Hi, I have to retrieve user information from active directory in ASP.NET by passing Employee ID to it. Can any one give me some information on how I should do this? Thanks!
0
by: mazdotnet | last post by:
Hi, I'm trying to get the 'User Information' by using the UserId and it doesn't work. This works, MembershipUser membershipUser = Membership.GetUser("username");
1
by: jerrydigital | last post by:
Hello, I am having trouble getting unique user information on my account page. Currently, once the user logs in, it will only display "Welcome...(then the same name pops up no matter who logs in)"....
2
by: Yene | last post by:
Am trying to use my user object to hold one value after the user has logged in my website. when loging in the user object id is set using user.setid("id"); Once this is done. I want to use...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.