ASP.NET Request.UserHostName不包含主机名 [英] ASP.NET Request.UserHostName not containing hostname

查看:813
本文介绍了ASP.NET Request.UserHostName不包含主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要给请求计算机的主机名存储在数据库中,如果在创建一个新的数据集。要清楚地表明这给用户(它是所有公司内部),我们显示这是在用户填写表单三个文本框。
这三个文本框都充满这样的:

I need to store the hostname of the requesting computer in the database if a new dataset is created. To clearly indicate this to the user (it's all company internal), we display this as three textboxes in the form the user fills out. These three textboxes are filled like that:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            txtHostname.Text = Request.UserHostName.ToString();
            txtIPAdress.Text = Request.UserHostAddress.ToString();
            txtWindowsLogin.Text = Request.LogonUserIdentity.Name.ToString();
        }
    }

然而无论从什么客户端我测试,这应该给在Request.UserHostname的主机名不工作,所以该领域充满了IP地址的IP的反向查找。如果我用nslookup在服务器上时,换工作正常。
对我来说任何提示,我可以开始?
非常感谢。

However no matter from what client I tested, the reverse lookup of the IP which should give the hostname in Request.UserHostname does not work, so the field is filled with the IP address. If I use nslookup on the server, the reversing works fine. Any hints for me where I could start? Thanks a lot.

推荐答案

您需要的 IIS配置得到这个工作。或者,您可以使用 Dns.GetHostEntry 做反向查找,如果你只需要在一个地方。

You need to configure IIS to get this working. Alternately, you can use Dns.GetHostEntry to do the reverse lookup if you only need it in one spot.

在每次请求反向查找会对性能产生严重影响,这就是为什么它是不是默认启用。如果可以的话,我建议在 Dns.GetHostEntry 路线。

Reverse lookup on every request can have a severe impact on performance, which is why it is not enabled by default. I'd recommend the Dns.GetHostEntry route if you can.

下面是我们使用了一个有用的反向查找方法:

Here's a helpful reverse lookup method we use:

public static string ReverseLookup(string ip)
{
    if (string.IsNullOrEmpty(ip)) return ip;

     try 
     {
       return Dns.GetHostEntry(ip).Select(entry => entry.HostName).FirstOrDefault() ?? ip;
     } 
     catch(SocketException) { return ip; }
}

这篇关于ASP.NET Request.UserHostName不包含主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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