Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get request origin in C# api controller

Tags:

Is there a way how can I can get request origin value in the api controller when I'm calling some api endpoint with ajax call?

For example I'm making this call from www.xyz.com:

$http({     url: 'http://myazurewebsite.azurewebsites.net/api/ValueCall/CheckForExistingValuers',     method: "GET",     params: { loanID: $scope.loanIdPopup } }).success(function (data) {  }).error(function (data) {  }); 

Once on the api side, how can I get the www.xyz.com value?

CORS is working properly.

like image 918
Laziale Avatar asked Dec 28 '16 16:12

Laziale


2 Answers

What you're looking for is probably the origin-header. All modern browsers send it along if you're doing a cross domain request.

In an ApiController you fetch it like so:

if (Request.Headers.Contains("Origin")) {     var values = Request.Headers.GetValues("Origin");     // Do stuff with the values... probably .FirstOrDefault() } 
like image 198
smoksnes Avatar answered Sep 20 '22 13:09

smoksnes


You can grab it from the API methods via current HTTP request headers collection:

  IEnumerable<string> originValues;   Request.Headers.TryGetValue("Origin", out originValues) 
like image 22
ivamax9 Avatar answered Sep 19 '22 13:09

ivamax9