我怎样才能获取客户端的主机名,本地计算机名和用户名在C#和asp.net

编程入门 行业动态 更新时间:2024-10-27 21:25:10
本文介绍了我怎样才能获取客户端的主机名,本地计算机名和用户名在C#和asp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的IP地址,但由于某些原因,我可以得到正确的名称解析,显示本地计算机名称。我尝试一些东西,他们都正显示出服务器主机名?

ipadd = HttpContext.Current.Request.ServerVariables [REMOTE_ADDR];    ip地址MYIP = IPAddress.Parse(ipadd);    IPHostEntry GetIPHost = Dns.GetHostEntry(MYIP);    // userhostname = System.Environment.MachineName;    // userhostname = Dns.GetHostName(); //解决ServerHostName没有电脑    // userhostname = HttpContext.Current.Request.UserHostName;    // userhostname = HttpContext.Current.Request.UserAgent;    userhostname = GetIPHost.HostName;

有关用户名,我特林使用

nametext = WindowsIdentity.GetCurrent()名称。//服务器部署时,显示服务器名称//当debuging和localY运行显示登录正确的当前用户// nametext = HttpContext.Current.Request.ServerVariables [LOGON_USER];//不返回任何东西

我交换身份验证和没有结果。

<身份验证模式=表格><身份验证模式=窗口>

解决方案

使用Htt$p$pquest.UserHostAddress和Htt$p$pquest.UserHostName客户端IP和机器名。

假设你有身份验证正确配置,你可以从IIdentity.Name.

在一个 页面的上下文 ,则可以使用 Request.UserHostAddress , Request.UserHostName 和 User.Identity.Name 。

I have the IP address, but for some reason I can get the name resolve correctly to show local computer name. I try few things and all of them is showing the server hostname?

ipadd = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; IPAddress myIP = IPAddress.Parse(ipadd); IPHostEntry GetIPHost = Dns.GetHostEntry(myIP); //userhostname = System.Environment.MachineName; //userhostname = Dns.GetHostName(); //Resolve ServerHostName not computer //userhostname = HttpContext.Current.Request.UserHostName; //userhostname = HttpContext.Current.Request.UserAgent; userhostname = GetIPHost.HostName;

For user name I am tring to use

nametext = WindowsIdentity.GetCurrent().Name; //Shows server name when deployed on server //when debuging and running localy shows correctly current user logged in //nametext = HttpContext.Current.Request.ServerVariables["LOGON_USER"]; // not returning anything

I switched authentication and no results

<authentication mode="Forms"> <authentication mode="Windows">

解决方案

Use HttpRequest.UserHostAddress and HttpRequest.UserHostName for client IP and machine name.

Assuming you have authentication configured correctly, you can get the client user from IIdentity.Name.

In the context of a Page, you can use Request.UserHostAddress, Request.UserHostName and User.Identity.Name.

更多推荐

我怎样才能获取客户端的主机名,本地计算机名和用户名在C#和asp.net

本文发布于:2023-05-25 10:51:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   主机名   用户名   计算机   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!