5

I am developing REST API application in ASP.NET MVC3. I am trying to get client client HOST Name in action but cannot success. I am using this API in multiple applications so I want to log the domain name.

Link 1

 HttpContext.Current.Request.UserHostAddress;
 System.Web.HttpContext.Current.Request.UserHostName;
 System.Web.HttpContext.Current.Request.UserHostAddress;

Link 2

private string GetClientIp(HttpRequestMessage request)
{
    if (request.Properties.ContainsKey("MS_HttpContext"))
    {
        return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
    }
    else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
    {
        RemoteEndpointMessageProperty prop;
        prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
        return prop.Address;
    }
    else
    {
        return null;
    }
}

All these options providing IP address but i want to get host name

API Controller www.myapi.com

public class TESTController : ApiController
{
    public TESTController()
    {
    }

    [HttpGet]
    public object Add(string data = "")
    {
        try
        {
            string result = "0";

            if (!string.IsNullOrEmpty(data))
            {
                //should be www.myapiconsume.com
                var host = System.Web.HttpContext.Current.Request.UserHostName;
                var ip = System.Web.HttpContext.Current.Request.UserHostAddress;
            }

            return new { response = result };
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
}

Calling API Action from different domain www.myapiconsume.com

 public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var url = "http://myapi.com/api/test/add?data={0}";
            url = string.Format(url, "{ data}");
            WebClient client = new WebClient();
            var result = client.DownloadString(url);
            return Content(result);
        }

    }

How can i get this?

Community
  • 1
  • 1
Shoaib Ijaz
  • 5,347
  • 12
  • 56
  • 84

3 Answers3

3

You can use Dns.GetHostEntry Method to reverse look up.

public static string ReverseLookup(string ip)
{
    if (String.IsNullOrWhiteSpace(ip))
        return ip;

    try
    {
        IPHostEntry host = Dns.GetHostEntry(ip);

        return host != null ? host.HostName : ip;
    }
    catch (SocketException)
    {
        return ip;
    }
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • I think there is no select method for Dns.GetHostEntry(ip). am i right? – Shoaib Ijaz Oct 21 '14 at 15:31
  • strange thing it is displaying Server Hosting Space name. as cpp.blue.sitehosted.com. this function displaying the "blue". – Shoaib Ijaz Oct 21 '14 at 15:54
  • DNS itself is a complex topic - forwarding, redirecting, internal, external. Once you publish to Production *(with public IP)*, it might be different compare to development machine *(with internal IP)*. However, looking up DNS based on IP address is not 100% reliable. My suggestion is your application not to rely on HostName. – Win Oct 21 '14 at 16:20
1

It isn't supplied by default, unfortunately; if you're on the same network, you can try:

var result = System.Net.Dns.GetHostEntry(System.Web.HttpContext.Current.Request.ServerVariables("remote_addr"));

but that may also have issues if the client is coming through a proxy, etc.

Allan Elder
  • 4,052
  • 17
  • 19
  • It providing the server name but i want domain name. Both applications hosted on same server but different website – Shoaib Ijaz Oct 21 '14 at 14:25
1

Update the calling action:

var url = "http://myapi.com/api/test/add?data={0}";
url = string.Format(url, "{ data}");
WebClient client = new WebClient();
client.Headers.Add("Referer", Request.Url.AbsoluteUri);
var result = client.DownloadString(url);
return Content(result);

then on target server:

Request.UrlReferrer.Host

If your server has a public ip then you can try to resolve it:

Dns.GetHostEntry(HttpContext.Current.Request.UserHostAddress)
Marian Ban
  • 8,158
  • 1
  • 32
  • 45