获取用户名 [英] Get user's name

查看:68
本文介绍了获取用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在写一个网络服务,我需要获得用户的名字

我可以从用户那里获得用户名。 Identity.Name

但是我想要显示名称


所以代替mydomain\jsmith而不是我想得到John Smith


有谁知道怎么做?


Vin


" Vincent Finn" < 1@2.com>在留言中写道

news:6l ******************************** @ 4ax.com ...



我正在写一个Web服务,我需要获取用户的名字
我可以从用户那里获得用户名。 Identity.Name
但我想要显示名称

因此,而不是mydomain\jsmith我想得到John Smith

有谁知道怎么做?

Vin



* Vincent Finn< 1@2.com> scripsit:

我正在写一个Web服务,我需要获取用户的名字
我可以从User.Identity.Name获取用户名
但是我想要显示名称

因此,而不是mydomain\jsmith我想得到John Smith




您是否尝试过''Environment.UserName''或''SystemInformation.UserName''?

请注意,后者需要引用

System.Windows.Forms.dll,并且这不是首选的服务方式

应用程序。


-

Herfried K. Wagner [MVP]

< URL:http://dotnet.mvps。 org />


不确定如何使用网络服务...


添加对系统的引用。 DirectoryServices


公共共享函数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作为SearchResult

Dim sResult As String

mySearcher .SearchScope = SearchScope.Subtree

mySearcher.ReferralChasing = ReferralChasingOption.All

mySearcher.Filter ="(&(objectClass = user)(sAMAccountName =" &

用户名& ))


尝试

oResult = mySearcher.FindOne

如果不是oResult则什么都没有

sResult =

oResult.GetDirectoryEntry.Properties(" DisplayName")。Value.ToString()

结束如果

Catch ex As Exception

抛出ex

结束尝试


oResult =没什么

mySearcher.Dispose( )

oDirectory.Dispose()


返回sResult

结束功能


Greg


" Vincent Finn" < 1@2.com>在留言中写道

news:6l ******************************** @ 4ax.com ...



我正在写一个Web服务,我需要获取用户的名字
我可以从用户那里获得用户名。 Identity.Name
但我想要显示名称

因此,而不是mydomain\jsmith我想得到John Smith

有谁知道怎么做?

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?

Vin

解决方案

MessageBox.Show(System.Windows.Forms.SystemInforma tion.UserName)

Regards - OHM

"Vincent Finn" <1@2.com> wrote in message
news:6l********************************@4ax.com...

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



* Vincent Finn <1@2.com> scripsit:

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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


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

"Vincent Finn" <1@2.com> wrote in message
news:6l********************************@4ax.com...

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



这篇关于获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆