且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取我已登录的计算机的帐户名

更新时间:2023-01-02 10:59:48

这取决于您将Asp.Net身份验证设置为哪种模式.这是一篇简短介绍选项的文章

http://www.4guysfromrolla.com/articles/031204-1.aspx [ ^ ]

现在,假设您要使用"Windows身份验证",因为您要获取连接到该站点的用户的帐户名.在这种情况下,您需要确保在web.config中设置了此模式,并且将IIS站点设置为使用集成身份验证"

然后,您可以使用类似的方法来获取Windows主体对象

//获取经过身份验证的用户的身份
WindowsPrincipal winPrincipal =(WindowsPrincipal)HttpContext.Current.User;

只需访问HttpContext.Current.User对象,您就可以得到想要的结果

进一步阅读

http://msdn.microsoft.com/en-us/library/ff647076.aspx [ ^ ]
It depends what mode you have your Asp.Net authentication set to. Here''s an article which briefly describes the options

http://www.4guysfromrolla.com/articles/031204-1.aspx[^]

Now, I''m assuming you want to work with ''Windows Authentication'' since you want to get the account name of the user connecting to the site. In which case, you''ll need to make sure this mode is set in your web.config and that you have your IIS site set to use ''Integrated Authentication''

You could then use something like this to get the Windows Principal object

// Obtain the authenticated user''s Identity
WindowsPrincipal winPrincipal = (WindowsPrincipal)HttpContext.Current.User;

Just accessing the HttpContext.Current.User object should give you what you want though

Further reading

http://msdn.microsoft.com/en-us/library/ff647076.aspx[^]


您可以通过Environment.MachineName


获取计算机的名称,我不确定您的应用程序是用于Internet还是Intranet.
如果要用于Internet,恐怕是不可能的.或者我应该说ASP.Net没有做到这一点的方法,您总是可以编写某种ActiveX控件来做到这一点.

现在是Intranet的东西.您可以尝试以下一种方法:

Well, I am not sure if you application is for Internet or Intranet.
If it is intended for internet, I am afraid that would not be possible. Or I should say ASP.Net does not have a way to do it, you can always write some kind of ActiveX control to do this.

Now comes the intranet thingie. You can try this one:

Dim host As System.Net.IPHostEntry
host = System.Net.Dns.GetHostByAddress(Request.ServerVariables.Item("REMOTE_HOST"))
string strComputerName = host.HostName