Определите имя компьютера клиента

Я утверждал бы, что глотание является меньшим из этих двух зла в этом сценарии, поскольку лучше повысить оригинал Exception - протест: , если , возможно, отказ чисто расположить самостоятельно довольно не чинят очень важный (возможно, если TransactionScope не мог бы расположить, так как это могло бы указать на отказ отката).

См. здесь для большего количества мыслей об этом - включая идею метода обертки/расширения:

using(var foo = GetDodgyDisposableObject().Wrap()) {
   foo.BaseObject.SomeMethod();
   foo.BaseObject.SomeOtherMethod(); // etc
} // now exits properly even if Dispose() throws

, Конечно, Вы могли также сделать некоторую причуду, где Вы повторно бросаете составное исключение и с оригиналом и с секунда (Dispose()) исключение - но думаете: у Вас могли быть приблизительно using блоки... это быстро станет неуправляемым. В действительности исходным исключением является интересное.

40
задан abatishchev 27 August 2014 в 03:35
поделиться

6 ответов

Я получил его, используя следующее:

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

код из 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();
}
49
ответ дан 27 November 2019 в 01:48
поделиться

No. The client's computer name is not available in any way on the server. This is the nature of the http request-response. You only can have its IP address.

A workarounds could be to retrieve machine on the client from Flash/Silverlight (I doubt JavaScript) and put in into cookie which is available on the server with each request. But there is a whole stack of issues with this approach.

3
ответ дан 27 November 2019 в 01:48
поделиться

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.

1
ответ дан 27 November 2019 в 01:48
поделиться

Try this:

string name = Request.UserHostName;
1
ответ дан 27 November 2019 в 01:48
поделиться

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
ответ дан 27 November 2019 в 01:48
поделиться

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>
-2
ответ дан 27 November 2019 в 01:48
поделиться
Другие вопросы по тегам:

Похожие вопросы: