【问题标题】:How to detect incoming request coming from another website如何检测来自另一个网站的传入请求
【发布时间】:2013-12-25 12:45:57
【问题描述】:

我有两个 asp.net 网站(比如 website1 和 website2)。需要根据某些用户操作将用户从一个网站重定向到另一个网站。有没有办法检测传入的请求是否来自 website1 到 website2。 我试图使用来自 website1 的查询字符串发出请求,但 website2 无法识别该查询字符串。

要为这个问题添加更多详细信息: 在网站 1:

protected void Button1_Click ( object sender, EventArgs e )
{
    Response.Redirect ( "http://website2.com/Home.aspx?empid=1" );
}

在 website2 home.aspx 中:

protected void page_init ( object sender, EventArgs e )
{
    string str=Request.QueryString ["empid"]; // str is coming as 'null'. 
}

提前致谢。 赛卡特

【问题讨论】:

  • 什么是查询字符串? “不被认可”是什么意思? website2 是否知道要在查询字符串中检查哪些请求参数?等等等等...显示一些代码会有所帮助。
  • 这里的情况:在 website1 中,我有 Response.Redirect("website2/a.aspx?empid=1") 我需要从请求中获取 "empid=1"。Request.QueryString 为空。website2知道查询参数。

标签: c# asp.net


【解决方案1】:

您可能希望在您的 website2 页面中使用 Request.UrlReferrer

if (Request.UrlReferrer != null) {
    variable = Request.UrlReferrer.toString();
}

if (variable.Contains("website1")) {
    // coming from website1
}

【讨论】:

  • 如果我没记错的话,Request.UrlReferrer 会给我请求来自的 url。这里的情况:在 website1 中,我有 Response.Redirect("website2/a.aspx?empid=1") 我需要从请求中获取 "empid=1"。
  • 我正在回复您未经编辑的问题:a way to detect if incoming request is coming from website1 to website2。编辑后很明显您正在寻找提取查询字符串。
【解决方案2】:

根据您的问题:

在网站 1:

protected void Button1_Click ( object sender, EventArgs e ) { 
   Response.Redirect ( "http://website2.com/Home.aspx?empid=1" ); 
} 

在 website2 home.aspx 中:

protected void page_init ( object sender, EventArgs e ) { 
    string str=Request.QueryString ["emp"]; // str is coming as 'null'. 
}

您在查询字符串中发送 empid,并且您正在阅读 Request.QueryString ["emp"]

更改两者中的一个以匹配另一个。

【讨论】:

  • 啊!我的错。这是一个错字,我实际上只在这两个地方检查 ["empid"]。
【解决方案3】:

您将empid 作为请求变量发送,然后搜索emp 变量。您应该更改网站2上的方法:

protected void page_init ( object sender, EventArgs e )
{
    string str=Request.QueryString ["empid"]; 
}

您可以使用this 帖子作为参考。

希望我能帮上忙!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 2020-02-22
    • 1970-01-01
    • 2010-12-29
    • 2015-08-16
    • 1970-01-01
    • 2018-04-13
    相关资源
    最近更新 更多