Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WEB api 2 getting client domain that is requesting

Hey all I am trying to find some code that will allow me to know what domain the POST request to the web service is coming from.

As an example:

If the web service is on domain:

bob.com/webService/Postsomething

And the client loads a page up on domain:

bill.com/postpage.html

Once the web service is clicked off using AJAX on the html page I want to be able to get the following information from the post function its calling:

bill.com

So far I have only been able to get the IP and host name of where the web service is on and not the client domain they are asking for information from the web service from.

like image 406
StealthRT Avatar asked Mar 14 '23 07:03

StealthRT


1 Answers

You could use the referrer HTTP header:

public HttpResponseMessage Get()
{
   var domain =  Request.Headers.Referrer?.GetLeftPart(UriPartial.Authority);

    ...
}

Of course this header is not guaranteed to be present and you absolutely cannot rely on it because the client making the HTTP request could simply decide not to send it. You should always check if it is null before using it.

like image 171
Darin Dimitrov Avatar answered Apr 07 '23 07:04

Darin Dimitrov