0

IN MVC 5 PROJECT suppose I have API like

abc.com/authonication?userName=asdklfsdf&token=ASDF45FEWF312DFSFWE465SDF465

And some another web app (example.com) use this API

In my controller, I want to get the host name (example.com) of domain who using this API to validate

Amit Singh Rawat
  • 559
  • 1
  • 9
  • 27
  • 3
    That's not how networks work. The request is issued from an IP address. You could do a reverse lookup to see which names are registered to that IP address, but that's not foolproof. See also [Reverse IP Domain Check?](https://stackoverflow.com/questions/716748/reverse-ip-domain-check). – CodeCaster Mar 07 '18 at 09:01
  • Is `example.com` calling your website from the client (the web browser) or the server (e.g. IIS)? – mjwills Mar 07 '18 at 09:27
  • example.com calling from browser – Amit Singh Rawat Mar 07 '18 at 10:52

2 Answers2

2

You can get some useful information from the HttpContext.Request field.

HttpContext.Request.UserHostAddress
HttpContext.Request.UserHostName

This information is not necessarily available, but in a lot of cases, it's there. Just don't depend on it.

Neil
  • 11,059
  • 3
  • 31
  • 56
  • from here HttpContext.Request.UserHostAddress i am gettting "ip address" and HttpContext.Request.UserHostName i am getting machine name – Amit Singh Rawat Mar 07 '18 at 09:13
  • 2
    As I said, it depends on many things, including the browser actually sending that information. Some browsers won't include it. For some requests, you WILL get the machine name, especially if you are trying it locally. – Neil Mar 07 '18 at 12:04
1

you can achieve this at your API Action method itself.

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

        if (!string.IsNullOrEmpty(data))
        {
            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;
    }

i found this here.

Coder
  • 406
  • 9
  • 22