如何知道哪个域使用JS发送请求到我的C#动作

时间:2015-10-14 09:01:37

标签: javascript c# asp.net-mvc http-referer

我需要知道哪个域向C#action(MVC)发送请求, 我收到请求并返回JS代码,

例如: 有人将此代码放入网站(http://www.ClientExampleSite.com/):

git clone openMVG
cd openMVG
git submodule update --init
mkdir build_dir && cd build_dir
CC=gcc-5 CXX=g++-5 cmake ../src

我需要返回此代码:

<script src="http://www.MyExampleSite.com/GetDomin /GetDomin" type="text/javascript"></script>

问题是UrlReferrer在这种情况下不起作用。

谢谢

编辑: 最后,我想做的是阻止来自未知域的请求。

2 个答案:

答案 0 :(得分:1)

你可以使用,

var domainName = System.Web.HttpContext.Current.Request.Url.Authority;

或者你可以尝试,

var theProvider = (IServiceProvider)HttpContext;
var theWorker = (HttpWorkerRequest)theProvider.GetService(typeof(HttpWorkerRequest));
string theReferer = theWorker.GetKnownRequestHeader(HttpWorkerRequest.HeaderReferer);  

<强>更新 根据您的评论,您可以从代码

下面的IP地址获取客户端域名
        var clientIP = (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]==null) ? System.Web.HttpContext.Current.Request.UserHostAddress:
                           System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        IPAddress addr = IPAddress.Parse(clientIP);
        IPHostEntry entry = Dns.GetHostEntry(addr);
        var clientDomainName = entry.HostName;

答案 1 :(得分:0)

你可以试试这个:

using System.Collections.ObjectModel;
using System.Windows;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;

namespace OxyPlot_TEST
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();

            // Create some data
            this.Items = new Collection<Item>
                            {
                                new Item {Label = "Apples", Value1 = 37},
                                new Item {Label = "Pears", Value1 = 7},
                                new Item {Label = "Bananas", Value1 = 23}
                            };

            // Create the plot model
            var tmp = new PlotModel { Title = "Column series", LegendPlacement = LegendPlacement.Outside, LegendPosition = LegendPosition.RightTop, LegendOrientation = LegendOrientation.Vertical };

            // Add the axes, note that MinimumPadding and AbsoluteMinimum should be set on the value axis.
            tmp.Axes.Add(new CategoryAxis { ItemsSource = this.Items, LabelField = "Label" });
            tmp.Axes.Add(new LinearAxis { Position = AxisPosition.Left, MinimumPadding = 0, AbsoluteMinimum = 0 });

            ColumnSeries bar = new ColumnSeries
            {
                FillColor = OxyPlot.OxyColors.Black,
                ValueField = "Value1",
                Title = "Value1",
                ItemsSource = Items
            };
            tmp.Series.Add(bar);

            this.Model1 = tmp;
            this.DataContext = this;
        }

        public Collection<Item> Items { get; set; }

        public PlotModel Model1 { get; set; }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Items.RemoveAt(0);
            Model1.InvalidatePlot(true);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Items.Add(new Item()
            {
                Label = "Strawberrys", Value1 = 55
            });
            Model1.InvalidatePlot(true);
        }
    }

    public class Item
    {
        public string Label { get; set; }
        public double Value1 { get; set; }
    }
}

参考Uri.Host Property

  

获取此实例的主机组件。

或者你可以试试这个:

Uri myUri = new Uri("http://www.ClientExampleSite.com/");   
string domain = myUri.Host;

获取客户端域名

var domain =   HttpContext.Current.Request.Url.Host;