Finding full name in Active Directory from a given username

G

Guest

Greetings,

I am writing an Intranet application in ASP.NET using VB.NET. I am
obtaining the username of the user with:

uName = User.Identity.Name, which is in the form of DOMAIN\username. I then
use substring to parse out just the username.

What I am trying to accomplish is getting the full name of the user by doing
a search in Active Directory, but I am failing at every turn. This code:

Dim de As DirectoryEntry
de = New DirectoryEntry("LDAP://Bestia/CN=" + uName) Bestia is the
name of our domain controller)
Dim ds As DirectorySearcher
ds = New DirectorySearcher(de)
Dim rs As SearchResult = ds.FindOne()

Returns this error:

[COMException (0x80072020): An operations error occurred]


This code:

Dim de As DirectoryEntry
de = New DirectoryEntry("LDAP://Bestia/CN=" + uName +
",DC=mainoffice") (mainoffice is the name of our domain)
Dim ds As DirectorySearcher
ds = New DirectorySearcher(de)

Returns this error:


[COMException (0x8007202b): A referral was returned from the server]


I'm new at doing this sort of coding and it seems like it should be a fairly
simple thing to do, but I can't get it to work. Can anybody help with this
problem? Also, if anybody knows of a simpler way to do this, I'm all ears.

Thanks in advance!

Mike
 
P

Paul Clement

¤ Greetings,
¤
¤ I am writing an Intranet application in ASP.NET using VB.NET. I am
¤ obtaining the username of the user with:
¤
¤ uName = User.Identity.Name, which is in the form of DOMAIN\username. I then
¤ use substring to parse out just the username.
¤
¤ What I am trying to accomplish is getting the full name of the user by doing
¤ a search in Active Directory, but I am failing at every turn. This code:
¤
¤ Dim de As DirectoryEntry
¤ de = New DirectoryEntry("LDAP://Bestia/CN=" + uName) Bestia is the
¤ name of our domain controller)
¤ Dim ds As DirectorySearcher
¤ ds = New DirectorySearcher(de)
¤ Dim rs As SearchResult = ds.FindOne()
¤
¤ Returns this error:
¤
¤ [COMException (0x80072020): An operations error occurred]
¤
¤
¤ This code:
¤
¤ Dim de As DirectoryEntry
¤ de = New DirectoryEntry("LDAP://Bestia/CN=" + uName +
¤ ",DC=mainoffice") (mainoffice is the name of our domain)
¤ Dim ds As DirectorySearcher
¤ ds = New DirectorySearcher(de)
¤
¤ Returns this error:
¤
¤
¤ [COMException (0x8007202b): A referral was returned from the server]
¤
¤
¤ I'm new at doing this sort of coding and it seems like it should be a fairly
¤ simple thing to do, but I can't get it to work. Can anybody help with this
¤ problem? Also, if anybody knows of a simpler way to do this, I'm all ears.
¤
¤ Thanks in advance!

If all you need is the FullName then use the WinNT provider instead of LDAP:

Dim DomainUser As String =
System.Security.Principal.WindowsIdentity.GetCurrent.Name.Replace("\", "/")
Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://" & DomainUser)

Dim FullName As String = ADEntry.Properties("FullName").Value


Paul
~~~~
Microsoft MVP (Visual Basic)
 
G

Guest

Paul Clement said:
¤ Greetings,
¤
¤ I am writing an Intranet application in ASP.NET using VB.NET. I am
¤ obtaining the username of the user with:
¤
¤ uName = User.Identity.Name, which is in the form of DOMAIN\username. I then
¤ use substring to parse out just the username.
¤
¤ What I am trying to accomplish is getting the full name of the user by doing
¤ a search in Active Directory, but I am failing at every turn. This code:
¤
¤ Dim de As DirectoryEntry
¤ de = New DirectoryEntry("LDAP://Bestia/CN=" + uName) Bestia is the
¤ name of our domain controller)
¤ Dim ds As DirectorySearcher
¤ ds = New DirectorySearcher(de)
¤ Dim rs As SearchResult = ds.FindOne()
¤
¤ Returns this error:
¤
¤ [COMException (0x80072020): An operations error occurred]
¤
¤
¤ This code:
¤
¤ Dim de As DirectoryEntry
¤ de = New DirectoryEntry("LDAP://Bestia/CN=" + uName +
¤ ",DC=mainoffice") (mainoffice is the name of our domain)
¤ Dim ds As DirectorySearcher
¤ ds = New DirectorySearcher(de)
¤
¤ Returns this error:
¤
¤
¤ [COMException (0x8007202b): A referral was returned from the server]
¤
¤
¤ I'm new at doing this sort of coding and it seems like it should be a fairly
¤ simple thing to do, but I can't get it to work. Can anybody help with this
¤ problem? Also, if anybody knows of a simpler way to do this, I'm all ears.
¤
¤ Thanks in advance!

If all you need is the FullName then use the WinNT provider instead of LDAP:

Dim DomainUser As String =
System.Security.Principal.WindowsIdentity.GetCurrent.Name.Replace("\", "/")
Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://" & DomainUser)

Dim FullName As String = ADEntry.Properties("FullName").Value


Paul
~~~~
Microsoft MVP (Visual Basic)

Hey Paul,

Thanks a lot for the help! What I get from this is "ASP.NET Machine
Account." I am guessing that is impersonation going on or something. What I
am looking for is the name of the user that is accessing the site. Any more
thoughts?

Thanks again!

Mike
 
P

Paul Clement

¤
¤
¤ "Paul Clement" wrote:
¤
¤ >
¤ > ¤ Greetings,
¤ > ¤
¤ > ¤ I am writing an Intranet application in ASP.NET using VB.NET. I am
¤ > ¤ obtaining the username of the user with:
¤ > ¤
¤ > ¤ uName = User.Identity.Name, which is in the form of DOMAIN\username. I then
¤ > ¤ use substring to parse out just the username.
¤ > ¤
¤ > ¤ What I am trying to accomplish is getting the full name of the user by doing
¤ > ¤ a search in Active Directory, but I am failing at every turn. This code:
¤ > ¤
¤ > ¤ Dim de As DirectoryEntry
¤ > ¤ de = New DirectoryEntry("LDAP://Bestia/CN=" + uName) Bestia is the
¤ > ¤ name of our domain controller)
¤ > ¤ Dim ds As DirectorySearcher
¤ > ¤ ds = New DirectorySearcher(de)
¤ > ¤ Dim rs As SearchResult = ds.FindOne()
¤ > ¤
¤ > ¤ Returns this error:
¤ > ¤
¤ > ¤ [COMException (0x80072020): An operations error occurred]
¤ > ¤
¤ > ¤
¤ > ¤ This code:
¤ > ¤
¤ > ¤ Dim de As DirectoryEntry
¤ > ¤ de = New DirectoryEntry("LDAP://Bestia/CN=" + uName +
¤ > ¤ ",DC=mainoffice") (mainoffice is the name of our domain)
¤ > ¤ Dim ds As DirectorySearcher
¤ > ¤ ds = New DirectorySearcher(de)
¤ > ¤
¤ > ¤ Returns this error:
¤ > ¤
¤ > ¤
¤ > ¤ [COMException (0x8007202b): A referral was returned from the server]
¤ > ¤
¤ > ¤
¤ > ¤ I'm new at doing this sort of coding and it seems like it should be a fairly
¤ > ¤ simple thing to do, but I can't get it to work. Can anybody help with this
¤ > ¤ problem? Also, if anybody knows of a simpler way to do this, I'm all ears.
¤ > ¤
¤ > ¤ Thanks in advance!
¤ >
¤ > If all you need is the FullName then use the WinNT provider instead of LDAP:
¤ >
¤ > Dim DomainUser As String =
¤ > System.Security.Principal.WindowsIdentity.GetCurrent.Name.Replace("\", "/")
¤ > Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://" & DomainUser)
¤ >
¤ > Dim FullName As String = ADEntry.Properties("FullName").Value
¤ >
¤ >
¤ > Paul
¤ > ~~~~
¤ > Microsoft MVP (Visual Basic)
¤ >
¤
¤ Hey Paul,
¤
¤ Thanks a lot for the help! What I get from this is "ASP.NET Machine
¤ Account." I am guessing that is impersonation going on or something. What I
¤ am looking for is the name of the user that is accessing the site. Any more
¤ thoughts?

You need to enable impersonation. I'm not sure what type of authentication your web application is
set up for.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsent7/html/vxconimpersonation.asp


Paul
~~~~
Microsoft MVP (Visual Basic)
 
G

Guest

Paul Clement said:
¤
¤
¤ "Paul Clement" wrote:
¤
¤ >
¤ > ¤ Greetings,
¤ > ¤
¤ > ¤ I am writing an Intranet application in ASP.NET using VB.NET. I am
¤ > ¤ obtaining the username of the user with:
¤ > ¤
¤ > ¤ uName = User.Identity.Name, which is in the form of DOMAIN\username. I then
¤ > ¤ use substring to parse out just the username.
¤ > ¤
¤ > ¤ What I am trying to accomplish is getting the full name of the user by doing
¤ > ¤ a search in Active Directory, but I am failing at every turn. This code:
¤ > ¤
¤ > ¤ Dim de As DirectoryEntry
¤ > ¤ de = New DirectoryEntry("LDAP://Bestia/CN=" + uName) Bestia is the
¤ > ¤ name of our domain controller)
¤ > ¤ Dim ds As DirectorySearcher
¤ > ¤ ds = New DirectorySearcher(de)
¤ > ¤ Dim rs As SearchResult = ds.FindOne()
¤ > ¤
¤ > ¤ Returns this error:
¤ > ¤
¤ > ¤ [COMException (0x80072020): An operations error occurred]
¤ > ¤
¤ > ¤
¤ > ¤ This code:
¤ > ¤
¤ > ¤ Dim de As DirectoryEntry
¤ > ¤ de = New DirectoryEntry("LDAP://Bestia/CN=" + uName +
¤ > ¤ ",DC=mainoffice") (mainoffice is the name of our domain)
¤ > ¤ Dim ds As DirectorySearcher
¤ > ¤ ds = New DirectorySearcher(de)
¤ > ¤
¤ > ¤ Returns this error:
¤ > ¤
¤ > ¤
¤ > ¤ [COMException (0x8007202b): A referral was returned from the server]
¤ > ¤
¤ > ¤
¤ > ¤ I'm new at doing this sort of coding and it seems like it should be a fairly
¤ > ¤ simple thing to do, but I can't get it to work. Can anybody help with this
¤ > ¤ problem? Also, if anybody knows of a simpler way to do this, I'm all ears.
¤ > ¤
¤ > ¤ Thanks in advance!
¤ >
¤ > If all you need is the FullName then use the WinNT provider instead of LDAP:
¤ >
¤ > Dim DomainUser As String =
¤ > System.Security.Principal.WindowsIdentity.GetCurrent.Name.Replace("\", "/")
¤ > Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://" & DomainUser)
¤ >
¤ > Dim FullName As String = ADEntry.Properties("FullName").Value
¤ >
¤ >
¤ > Paul
¤ > ~~~~
¤ > Microsoft MVP (Visual Basic)
¤ >
¤
¤ Hey Paul,
¤
¤ Thanks a lot for the help! What I get from this is "ASP.NET Machine
¤ Account." I am guessing that is impersonation going on or something. What I
¤ am looking for is the name of the user that is accessing the site. Any more
¤ thoughts?

You need to enable impersonation. I'm not sure what type of authentication your web application is
set up for.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsent7/html/vxconimpersonation.asp


Paul
~~~~
Microsoft MVP (Visual Basic)

Hey Paul,

I set impersonation on by adding this line in the web.config file: <identity
impersonate="true" />, AND IT WORKS! I have the site set up for Windows
Authentication.

It's clear I have a lot to learn about this!

Thanks a lot for taking the time.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top