0

as mentioned above after doing lot of try & error I still stuck at this, all the reference I found till now still unable to solve this issue as I can't get the real computer name at all, instead most of example I tried before will return server name or the closest one will return "10" no matter how many user it captured. I'm making a log function for my system, so far I can get domain name, user ip address, user id like in my code below :

Please somebody guide me on how to get "User Computer Name"...TQ Everyone

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();
    }

Example of the output like this :

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

UPDATE :

I manage to get the answer now, the answer link to my answer on another post here :

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

An updated version from Kelsey post on the link above :

$(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;
});

To store the value, add these control :

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

Where you also can calling it from behind like this :

Page.ClientScript.RegisterStartupScript(this.GetType(), "MachineInfo", "GetInfo();", true);
Community
  • 1
  • 1
mutanic
  • 2,448
  • 1
  • 15
  • 17
  • To attract knowledgeable users, please tag the question with the language and tools you're using. – paulmelnikow Feb 18 '13 at 04:32
  • sorry, I did try to tag it well but it giving me error as my reputation is not enough yet. somehow after i paste the code it didn't show in colour like usual. can someone help to fix it? – mutanic Feb 18 '13 at 04:42
  • I mean, is it C# and ASP.NET? – paulmelnikow Feb 18 '13 at 17:23
  • Yes it is, somehow when user present is captured & system suppose to create an access log, it wont create a log file probably due user authorization level but it's fine when I use my admin ID & the log file will be created whenever I login. So I had to change my strategy & switch to create a log into Sharepoint list. I notice something which I will mention about it in separate question after this. The conclusion for my original question is : 1) I fail to get user computer name using C#.Net 2) I manage to get user computer name using Javascript. Thanks everyone – mutanic Feb 19 '13 at 01:33

2 Answers2

0

Well, you're setting LocalComputerName to

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

What happens if you change that to:

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

If you end up with an IP address, try using nslookup on that and seeing if you get a hostname. If you don't, your problem is in DNS resolution.

By the way, does the actual IP address start with 10. ?

paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
  • I did try that one & it give me an IP Address instead of Computer name. But you get my attention when asking me either the IP Address is start with 10, infact the answwer is Yes, after checking the rest of logs yes it give the 1st IP header as user computer name. How do I solve this? – mutanic Feb 18 '13 at 04:16
  • 1
    So, it's the DNS resolver on your computer which is responsible for resolving 10.whatever to a hostname, and the result you're getting suggests that it can't do that. It's just returning the hostname, then you're cutting it off at the first `.` and getting `10`. To fix this, you'll need to add PTR records to your DNS server which map the IP addresses back to the host names. In other words, the way a TCP connection works, it's not always possible to get the hostname of the remote computer. – paulmelnikow Feb 18 '13 at 04:25
  • Hurmmmm...you might be right, is it possible if I'm do it another way such as using Javascript to get user computer name & store it into a hidden textbox then use the value received. Is there such script exist? – mutanic Feb 18 '13 at 04:30
  • I mean use javascript to get the user/client computer name & store it to a textbox, then use the textbox value to capture into log. – mutanic Feb 18 '13 at 04:35
  • 1
    So the client is a web browser? Sure, that's worth a try. – paulmelnikow Feb 18 '13 at 04:38
0

I'm getting close now, here is the code for my .ascx file :

<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>

And this the textbox to hold the computer name, it suppose to be hidden but it won't work if it's hidden.

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

This is the code behind :

    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();
        }
    }

But the problem is, txtPCName cannot be hidden either by using Visible="false" or Style="display: none" cause it will not be able to store the value from Javascript to txtPCName. How do I solve this? I cannot let the textbox showing on the page.

mutanic
  • 2,448
  • 1
  • 15
  • 17
  • This has become an entirely different question – how to store a value from JavaScript in a hidden form field. Would you accept my answer and post – or search for – a new question with just the essential detail? – paulmelnikow Feb 18 '13 at 17:22
  • Your 1st post as answer? It might not solve my issue but thanks for sharing your knowledge, I'll mark it as answer. – mutanic Feb 19 '13 at 01:26