Determine Client's Computer Name

后端 未结 7 656
情歌与酒
情歌与酒 2020-11-29 23:41

I am building an intranet site that will display different lists based on the computer name because different computers are in different areas, is there a way (within a cont

相关标签:
7条回答
  • 2020-11-30 00:16

    Here's an IE-only solution. It works in IE8, with multiple security warnings.

    <script type="text/javascript" language="javascript">
       var ax = new ActiveXObject("WScript.Network");
       document.write(ax.UserName + '<br />'); //logged in account name
       document.write(ax.ComputerName + '<br />'); //Windows PC name
    </script>
    
    0 讨论(0)
  • 2020-11-30 00:27

    code in VB :

    Dim myIP As IPAddress = IPAddress.Parse(Request.UserHostName)
        Dim GetIPHost As IPHostEntry = Dns.GetHostEntry(myIP)
        Dim compName As List(Of String) = GetIPHost.HostName.ToString.Split("").ToList
    
        return(compName.First)
    
    0 讨论(0)
  • 2020-11-30 00:27

    The only way I know of to inspect the client is through the ServerVariables collection on the Request object (should be available for MVC code).

    See http://www.4guysfromrolla.com/webtech/092298-3.shtml for more information. REMOTE_HOST and REMOTE_ADDR look like candidates.

    0 讨论(0)
  • Try this:

    string name = Request.UserHostName;
    
    0 讨论(0)
  • 2020-11-30 00:29

    I got it working using the following:

    string IP = Request.UserHostName;
    string compName = CompNameHelper.DetermineCompName(IP);
    

    code from compnamehelper:

    public static string DetermineCompName(string IP)
    {
        IPAddress myIP = IPAddress.Parse(IP);
        IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
        List<string> compName = GetIPHost.HostName.ToString().Split('.').ToList();
        return compName.First();
    }
    
    0 讨论(0)
  • 2020-11-30 00:30

    I think you are better off using one of these methods to tie a user to a location:

    • a cookie that is set once the user self-selects their location
    • having the user login to the site so that you can track them uniquely that way
    • remembering user by IP address

    There is no way of ensuring remote hostnames are unique. The same issue occurs with IP because of proxies, dynamic IP, etc., but I think it will be a little more reliable. Also, you can do geolocation by IP address.

    0 讨论(0)
提交回复
热议问题