Get request origin in C# api controller

為{幸葍}努か 提交于 2020-05-10 07:31:07

问题


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.


回答1:


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()
}



回答2:


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

  IEnumerable<string> originValues;
  Request.Headers.TryGetValue("Origin", out originValues)


来源:https://stackoverflow.com/questions/41365670/get-request-origin-in-c-sharp-api-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!