0

上記のように、多くの試行錯誤を行った後、私はまだこれに固執しています。実際のコンピューター名をまったく取得できないため、これまでに見つけたすべての参照はまだこの問題を解決できません。代わりに、以前に試した例のほとんどはサーバー名を返すか、最も近いサーバー名は、キャプチャしたユーザーの数に関係なく「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);
4

2 に答える 2

0

さて、あなたはに設定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.ですか?

于 2013-02-18T04:10:48.650 に答える
0

私は今近づいています.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();
        }
    }

しかし問題は、Visible="false" または Style="display: none" を使用しても txtPCName を非表示にできないため、Javascript から txtPCName に値を保存できないことです。これを解決するにはどうすればよいですか?ページにテキストボックスを表示できません。

于 2013-02-18T06:00:54.057 に答える