Get user's name

  • Thread starter Thread starter Vincent Finn
  • Start date Start date
V

Vincent Finn

Hi,

I am writting a web service and I need to get the User's name
I can get the username from User.Identity.Name
but I want the display name

So instead of "mydomain\jsmith" I want to get "John Smith"

Does anyone know how to do this?

Vin
 
MessageBox.Show(System.Windows.Forms.SystemInformation.UserName)

Regards - OHM
 
* Vincent Finn said:
I am writting a web service and I need to get the User's name
I can get the username from User.Identity.Name
but I want the display name

So instead of "mydomain\jsmith" I want to get "John Smith"

Did you try 'Environment.UserName' or 'SystemInformation.UserName'?
Notice that the latter would require a reference to
"System.Windows.Forms.dll", and that's not the preferred way for service
applications.
 
Not sure how this plays out using a web service...

add a reference to System.DirectoryServices

Public Shared Function GetFullName(ByVal UserName As String) As String
Const DomainName As String = "yourdomain.com"

Dim oDirectory As New DirectoryEntry("LDAP://" & DomainName)
Dim mySearcher As New DirectorySearcher(oDirectory)
Dim oResult As SearchResult
Dim sResult As String
mySearcher.SearchScope = SearchScope.Subtree
mySearcher.ReferralChasing = ReferralChasingOption.All
mySearcher.Filter = "(&(objectClass=user)(sAMAccountName=" &
UserName & "))"

Try
oResult = mySearcher.FindOne
If Not oResult Is Nothing Then
sResult =
oResult.GetDirectoryEntry.Properties("DisplayName").Value.ToString()
End If
Catch ex As Exception
Throw ex
End Try

oResult = Nothing
mySearcher.Dispose()
oDirectory.Dispose()

Return sResult
End Function

Greg
 
MessageBox.Show(System.Windows.Forms.SystemInformation.UserName)

Returns ASPNET as the user since that's the user asp runs under
I need the user that is accessing the web client

That's what "User.Identity.Name" gives but no display name

Vin
 
Did you try 'Environment.UserName' or 'SystemInformation.UserName'?
Notice that the latter would require a reference to
"System.Windows.Forms.dll", and that's not the preferred way for service
applications.

They return ASPNET as the user since that's the user asp runs under
I need the user that is accessing the web client

That's what "User.Identity.Name" gives but no display name

Vin
 
Theres always the win32 API function. Although not sure what user it would
return either as the person logging into a website becomes IUSR_....

Shane
 
¤ Hi,
¤
¤ I am writting a web service and I need to get the User's name
¤ I can get the username from User.Identity.Name
¤ but I want the display name
¤
¤ So instead of "mydomain\jsmith" I want to get "John Smith"
¤
¤ Does anyone know how to do this?

Try using the System.DirectoryServices namespace:

Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://DomainName/UserID")

Console.WriteLine(ADEntry.Properties("FullName").Value)


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
I get an exception if I use that

Message "An operations error occurred"

StackTrace " at
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean
findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()
at RAPIDWS.RAPIDWS.GetFullName(String FullUserName) in
C:\Inetpub\wwwroot\RAPIDWS\RAPIDWS.asmx.vb:line 1016"

If I change the provider I get different exceptions but not really any
better
WinNT gives

Message "The provider does not support searching and cannot search
WinNT://DomainName."

StackTrace " at
System.DirectoryServices.DirectorySearcher.FindAll(Boolean
findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()
at RAPIDWS.RAPIDWS.GetFullName(String FullUserName) in
C:\Inetpub\wwwroot\RAPIDWS\RAPIDWS.asmx.vb:line 1016"


Any idea what might be causing that?
or suggestions on where to find good information on it since the MSDN
isn't very good?

Vin
 
I have code which I use elsewhere which calls the APIs
that works in normal applications
but that fails when called from the web service

Vin
 
¤ Hi,
¤
¤ I am writting a web service and I need to get the User's name
¤ I can get the username from User.Identity.Name
¤ but I want the display name
¤
¤ So instead of "mydomain\jsmith" I want to get "John Smith"
¤
¤ Does anyone know how to do this?

Try using the System.DirectoryServices namespace:

Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://DomainName/UserID")

Console.WriteLine(ADEntry.Properties("FullName").Value)


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)

The throws an exception
"The network path was not found"

I tried putting the username (and password) in seperately and the
exception goes away but the FullName returns Nothing

Dim sDir As String = "WinNT://" + DomainName
Dim ADEntry As New System.DirectoryServices.DirectoryEntry(sDir)
ADEntry.Username = UserName
ADEntry.Password = Password
Dim sFullName As String = ADEntry.Properties("FullName").Value

sFullName will be Nothing

Vin
 
¤ On Thu, 10 Jun 2004 13:18:19 -0500, Paul Clement
¤
¤ >
¤ >¤ Hi,
¤ >¤
¤ >¤ I am writting a web service and I need to get the User's name
¤ >¤ I can get the username from User.Identity.Name
¤ >¤ but I want the display name
¤ >¤
¤ >¤ So instead of "mydomain\jsmith" I want to get "John Smith"
¤ >¤
¤ >¤ Does anyone know how to do this?
¤ >
¤ >Try using the System.DirectoryServices namespace:
¤ >
¤ > Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://DomainName/UserID")
¤ >
¤ > Console.WriteLine(ADEntry.Properties("FullName").Value)
¤ >
¤ >
¤ >Paul ~~~ (e-mail address removed)
¤ >Microsoft MVP (Visual Basic)
¤
¤ The throws an exception
¤ "The network path was not found"
¤
¤ I tried putting the username (and password) in seperately and the
¤ exception goes away but the FullName returns Nothing
¤
¤ Dim sDir As String = "WinNT://" + DomainName
¤ Dim ADEntry As New System.DirectoryServices.DirectoryEntry(sDir)
¤ ADEntry.Username = UserName
¤ ADEntry.Password = Password
¤ Dim sFullName As String = ADEntry.Properties("FullName").Value
¤
¤ sFullName will be Nothing

I have no idea what your domain name and user name is. You need to supply those in the directory
entry string. Check with your network people if you're not sure what the domain name is.

You don't need to supply the password in order to retrieve the property value.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
¤ Dim sDir As String = "WinNT://" + DomainName
¤ Dim ADEntry As New System.DirectoryServices.DirectoryEntry(sDir)
¤ ADEntry.Username = UserName
¤ ADEntry.Password = Password
¤ Dim sFullName As String = ADEntry.Properties("FullName").Value
¤
¤ sFullName will be Nothing

I have no idea what your domain name and user name is. You need to supply those in the directory
entry string. Check with your network people if you're not sure what the domain name is.

I have the domain name in the variable called DomainName above
So it is part of the directory entry string

sDir = "WinNT://AUTOMSOFT"
when it is passed in so it is correct
You don't need to supply the password in order to retrieve the property value.

I didn't think so but I am clutching at straws at this stage

If I don't give the password it thorws an exception

Vin
 
¤ On Tue, 15 Jun 2004 09:51:11 -0500, Paul Clement
¤
¤ >¤ Dim sDir As String = "WinNT://" + DomainName
¤ >¤ Dim ADEntry As New System.DirectoryServices.DirectoryEntry(sDir)
¤ >¤ ADEntry.Username = UserName
¤ >¤ ADEntry.Password = Password
¤ >¤ Dim sFullName As String = ADEntry.Properties("FullName").Value
¤ >¤
¤ >¤ sFullName will be Nothing
¤ >
¤ >I have no idea what your domain name and user name is. You need to supply those in the directory
¤ >entry string. Check with your network people if you're not sure what the domain name is.
¤
¤ I have the domain name in the variable called DomainName above
¤ So it is part of the directory entry string
¤
¤ sDir = "WinNT://AUTOMSOFT"
¤ when it is passed in so it is correct
¤
¤ >You don't need to supply the password in order to retrieve the property value.
¤
¤ I didn't think so but I am clutching at straws at this stage
¤
¤ If I don't give the password it thorws an exception

If you've already authenticated with the network I'm surprised that you need to supply a password.
Perhaps they've locked down query access to AD.

Regarding the FullName property, I would verify that it has actually been entered for the users.
It's possible the property was been left blank when users were added to the system. You could also
try the FirstName and LastName properties.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
If you've already authenticated with the network I'm surprised that you need to supply a password.
Perhaps they've locked down query access to AD.

Regarding the FullName property, I would verify that it has actually been entered for the users.
It's possible the property was been left blank when users were added to the system. You could also
try the FirstName and LastName properties.

Finally have a solution
I don't know how I missed trying this one when I tried all the other
combination

It is essentailly the same as your original suggestion except that I
have to set the UserName and Password properties

Dim sDir As String = "WinNT://" + DomainName + "/" + UserName
Dim ADEntry As New System.DirectoryServices.DirectoryEntry(sDir)
ADEntry.Username = UserName
ADEntry.Password = Password
Dim sss As String = ADEntry.Properties("FullName").Value

This seems odd to me since the username is set in the directory string
but it works so I'll take it

Vin
 
Back
Top