【问题标题】:Unable to get "user computer name"无法获取“用户计算机名”
【发布时间】:2013-02-18 04:02:45
【问题描述】:

如上所述,在做了很多尝试和错误之后,我仍然坚持这一点,到目前为止我发现的所有参考仍然无法解决这个问题,因为我根本无法获得真实的计算机名称,而是大多数示例我之前尝试过将返回服务器名称,或者最近的一个将返回“10”,无论它捕获了多少用户。我正在为我的系统创建一个日志功能,到目前为止,我可以在下面的代码中获取域名、用户 IP 地址、用户 ID:

请有人指导我如何获取“用户计算机名”...TQ 大家

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Mail;
using System.IO;
using System.Globalization;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~

    {
        SPSite spSite = SPContext.Current.Site;
        SPSite spConvSite = new SPSite("www.mywebsite.com");
        SPWeb spWeb = SPContext.Current.Web;
        SPWeb spConvWeb = spConvSite.OpenWeb();
        SPUser spUser = spWeb.CurrentUser;

        string strDateLogCreated = DateTime.Now.ToString("ddMMyyyy-hhmmsstt", CultureInfo.InvariantCulture);
        string LogFolder = "D:\\Logs\\Client";
        string[] LocalComputerName = System.Net.Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName.Split(new Char[] { '.' });
        String ecn = System.Environment.MachineName;
        string domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
        string hostName = Dns.GetHostName();
        IPGlobalProperties  ipProperties = IPGlobalProperties.GetIPGlobalProperties();
        string testHost = ipProperties.HostName;
        string testDomain = ipProperties.DomainName;
        string strLocalIPAddress = HttpContext.Current.Request.UserHostAddress.ToString();

        string hName = "";      
        System.Net.IPHostEntry host = new System.Net.IPHostEntry();
        host = System.Net.Dns.GetHostEntry(HttpContext.Current.Request.ServerVariables["REMOTE_HOST"]);

        //Split out the host name from the FQDN
        if (host.HostName.Contains("."))
        {
        string[] sSplit = host.HostName.Split('.');
        hName = sSplit[0].ToString();
        }
        else
        {
        hName = host.HostName.ToString();
        }
        //return hName;


        StreamWriter swLogFile = File.AppendText(String.Format(@"{0}\Client-{1}.log", LogFolder, strDateLogCreated));

        swLogFile.WriteLine("*** Date & Time : " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture));
        swLogFile.WriteLine("*** Initiated By : " + spUser.Name.ToString());
        swLogFile.WriteLine("*** LAN ID : " + spUser.LoginName.ToString());
        swLogFile.WriteLine("*** Computer Name : " + LocalComputerName[0].ToString());
        swLogFile.WriteLine("*** IP Address : " + strLocalIPAddress.ToString());
        swLogFile.WriteLine("---------------------------------------------------");
        swLogFile.WriteLine(DateTime.Now.ToString("hh:mm:ss tt", CultureInfo.InvariantCulture) + " - " + "Successfully captured.");
        swLogFile.WriteLine("=====================================================================================================");
        swLogFile.WriteLine("Test 1 : " + domainName.ToString());
        swLogFile.WriteLine("Test 2 : " + hostName.ToString());
        swLogFile.WriteLine("Test 3 : " + testHost.ToString());
        swLogFile.WriteLine("Test 4 : " + System.Environment.GetEnvironmentVariable("COMPUTERNAME"));
        swLogFile.WriteLine("Test 5 : " + System.Net.Dns.GetHostEntry(Request.ServerVariables["REMOTE_HOST"]).HostName);
        swLogFile.WriteLine("Test 6 : " + hName.ToString());
        swLogFile.Flush();
    }

这样的输出示例:

Date & Time : 18/02/2013 11:45:26 AM

Initiated By : USER NAME

LAN ID : DOMAIN\ID99999

Computer Name : 10 //I don't get the real user computer name here!

IP Address : 99.999.99.999

//-----------------------------------//

11:45:26 AM - Successfully captured.

//===================================//

Test 1 : DOMAIN.COMPANYDOMAIN.com

Test 2 : SERVERNAME

Test 3 : SERVERNAME

Test 4 : SERVERNAME

Test 5 : 99.999.99.999

Test 6 : 10 //Also return the same result

更新:

我现在设法得到答案,我在另一个帖子上的答案的答案链接:

https://stackoverflow.com/a/38953637/1785641

Kelsey 在上面的链接上发布的更新版本:

$(function GetInfo() {
    var network = new ActiveXObject('WScript.Network');
        alert('User ID : ' + network.UserName + '\nComputer Name : ' + network.ComputerName + '\nDomain Name : ' + network.UserDomain);
        document.getElementById('<%= currUserID.ClientID %>').value = network.UserName;
        document.getElementById('<%= currMachineName.ClientID %>').value = network.ComputerName;
        document.getElementById('<%= currMachineDOmain.ClientID %>').value = network.UserDomain;
});

要存储值,请添加这些控件:

<asp:HiddenField ID="currUserID" runat="server" /> <asp:HiddenField ID="currMachineName" runat="server" /> <asp:HiddenField ID="currMachineDOmain" runat="server" />

你也可以像这样从后面调用它:

Page.ClientScript.RegisterStartupScript(this.GetType(), "MachineInfo", "GetInfo();", true);

【问题讨论】:

  • 为了吸引知识渊博的用户,请用您使用的语言和工具标记问题。
  • 对不起,我确实尝试很好地标记它,但它给了我错误,因为我的声誉还不够。不知何故,在我粘贴代码后,它不像往常一样以颜色显示。有人可以帮忙解决吗?
  • 我的意思是,是 C# 和 ASP.NET 吗?
  • 是的,当用户在场被捕获并且系统假设创建访问日志时,它不会创建日志文件,可能是由于用户授权级别,但是当我使用我的管理员 ID 和日志时它很好每当我登录时都会创建文件。所以我不得不改变我的策略并切换到在 Sharepoint 列表中创建一个日志。我注意到在此之后我将在单独的问题中提到它。我最初问题的结论是:1)我无法使用 C#.Net 获取用户计算机名称 2)我设法使用 Javascript 获取用户计算机名称。谢谢大家

标签: logging localhost computer-name


【解决方案1】:

好吧,你将 LocalComputerName 设置为

System.Net.Dns.GetHostEntry(
Request.ServerVariables["remote_addr"]).HostName.Split(new Char[] { '.' })

如果将其更改为:

System.Net.Dns.GetHostEntry(Request.ServerVariables["remote_addr"])

如果您最终获得了 IP 地址,请尝试在其上使用 nslookup 并查看您是否获得了主机名。否则,您的问题在于 DNS 解析。

顺便问一下,实际IP地址是不是以10.开头的?

【讨论】:

  • 我确实试过了,它给了我一个 IP 地址而不是计算机名称。但是当你问我 IP 地址是否以 10 开头时,你会引起我的注意,事实上答案是肯定的,在检查了其余的日志之后是的,它给出了第一个 IP 标头作为用户计算机名称。我该如何解决这个问题?
  • 因此,您计算机上的 DNS 解析器负责将 10.whatever 解析为主机名,而您得到的结果表明它无法做到这一点。它只是返回主机名,然后你在第一个. 处将其切断并获得10。要解决此问题,您需要将 PTR 记录添加到 DNS 服务器,将 IP 地址映射回主机名。换句话说,TCP 连接的工作方式并不总是可以获得远程计算机的主机名。
  • Hurmmmm ...您可能是对的,如果我以另一种方式(例如使用 Javascript 获取用户计算机名称并将其存储到隐藏的文本框中,然后使用收到的值)是否有可能。有这样的脚本吗?
  • 我的意思是使用 javascript 获取用户/客户端计算机名称并将其存储到文本框,然后使用文本框值捕获到日志中。
  • 那么客户端是网络浏览器?当然,这值得一试。
【解决方案2】:

我现在接近了,这是我的 .ascx 文件的代码:

<style type="text/css">
    .hiddenText
    {
        display: none;
    }
</style>
<script type="text/jscript">
<!--
    $(document).ready(function () {
        var net = new ActiveXObject("WScript.Network");
        var vPCName = net.ComputerName;
        document.getElementById('<%= txtPCName.ClientID %>').value = vPCName;
    });
//-->
</script>

这个是保存计算机名称的文本框,它应该是隐藏的,但如果隐藏它就不起作用了。

<asp:TextBox ID="txtPCName" runat="server" />

这是后面的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        Collector();
    }
    protected void Collector()
    {
        if (txtPCName.Text != null || txtPCName.Text != "")
        {
            SPSite spSite = SPContext.Current.Site;
            SPSite spConvSite = new SPSite("https://www.mywebsite.com");
            SPWeb spWeb = SPContext.Current.Web;
            SPWeb spConvWeb = spConvSite.OpenWeb();
            SPUser spUser = spWeb.CurrentUser;

            string strDateLogCreated = DateTime.Now.ToString("ddMMyyyy-hhmmsstt", CultureInfo.InvariantCulture);
            string LogFolder = "D:\\Logs\\Client";
            string[] LocalComputerName = System.Net.Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName.Split(new Char[] { '.' });
            String ecn = System.Environment.MachineName;
            string domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
            string hostName = Dns.GetHostName();
            string strLocalIPAddress = HttpContext.Current.Request.UserHostAddress.ToString();

            StreamWriter swLogFile = File.AppendText(String.Format(@"{0}\Client-{1}.log", LogFolder, strDateLogCreated));

            swLogFile.WriteLine("*** Date & Time : " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture));
            swLogFile.WriteLine("*** Initiated By : " + spUser.Name.ToString());
            swLogFile.WriteLine("*** LAN ID : " + spUser.LoginName.ToString());
            swLogFile.WriteLine("*** Computer Name : " + txtPCName.Text);
            swLogFile.WriteLine("*** IP Address : " + strLocalIPAddress.ToString());
            swLogFile.WriteLine("---------------------------------------------------");
            swLogFile.WriteLine(DateTime.Now.ToString("hh:mm:ss tt", CultureInfo.InvariantCulture) + " - " + "Successfully captured.");
            swLogFile.WriteLine("=====================================================================================================");
            swLogFile.Flush();
        }
    }

但问题是,txtPCName 无法通过使用 Visible="false" 或 Style="display: none" 隐藏,因为它无法将 Javascript 中的值存储到 txtPCName。我该如何解决这个问题?我不能让文本框显示在页面上。

【讨论】:

  • 这已成为一个完全不同的问题——如何将 JavaScript 中的值存储在隐藏的表单字段中。您会接受我的回答并发布(或搜索)一个仅包含基本细节的新问题吗?
  • 你的第一个帖子作为答案?它可能无法解决我的问题,但感谢您分享您的知识,我会将其标记为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多