4

I've tried with

System.Net.Dns.GetHostEntry(HttpContext.Connection.RemoteIpAddress.ToString()).ToString();

but that didn't work. I've also tried to use

PCName = Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"]).HostName; 

but this seems to be deprecated for Core

awh112
  • 1,466
  • 4
  • 22
  • 34
  • 2
    Maybe [this](https://stackoverflow.com/questions/1444592/determine-clients-computer-name)? – Grizzly Jun 07 '18 at 13:35
  • On the LAN, right? – ProgrammingLlama Jun 07 '18 at 13:39
  • @M12Bennett i had previously stumbled on that page but i can't seem to find the namespaces for those methods –  Jun 07 '18 at 14:01
  • Which method are you referring to? – Grizzly Jun 07 '18 at 14:03
  • @M12Bennett Request.UserHostName; comppNameHelper; also IPAddress and IPHostEntry i have included the following namespaces in my controller using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using MyModel; using Microsoft.AspNetCore.Http; using Newtonsoft.Json; using System.Web; –  Jun 07 '18 at 14:22
  • Click on `IPAddress`, press `Ctrl`+`.`, import the namespace it suggests. – ProgrammingLlama Jun 07 '18 at 14:27
  • @john than you for the tip, i didnt't know about that and it looks really helpful, sadly there is no namespace suggestion for `Request.UserHostName` and `comppNameHelper`. None of the Other suggestions i get seem to work –  Jun 07 '18 at 15:04
  • `Request.UserHostName` is `Request.HttpContext.Connection.RemoteIpAddress` in ASP.Net Core. `CompNameHelper` is the static class in which you've placed the `DetermineCompName` method. You'll have to call it according to wherever you've placed it. It's worth noting that no method (except client participation) is likely to give you the client hostname except where the client is on the LAN. – ProgrammingLlama Jun 07 '18 at 15:15
  • @john this application should mostly work on a LAN, if you have better ideas to get a semi-unique identifier from a client without making them log in i'm all ears. Im not sure what you mean for where i placed `DetermineCompName` , i never did such thing –  Jun 07 '18 at 15:16
  • The method [here](https://stackoverflow.com/a/1445109/3181933) is called `DetermineCompName`, is it not? – ProgrammingLlama Jun 07 '18 at 15:18

2 Answers2

5

Try this.

using Microsoft.AspNetCore.Http;
using System.Net;

Dns.GetHostEntry(HttpContext.Connection.RemoteIpAddress).HostName;
Dharman
  • 30,962
  • 25
  • 85
  • 135
0
httpContext.Features.Get<IServerVariablesFeature>()["REMOTE_HOST"]

Works on Windows only, because check this SO answer

Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149