【问题标题】:get referrer from a specific page in same domain C#从同一域 C# 中的特定页面获取引荐来源网址
【发布时间】:2022-01-06 18:18:37
【问题描述】:

我的站点有default.aspx 页面,您单击提交并进入customerinfo.aspx 页面。但是,它们必须来自同一域中的 default.aspx 页面。如果引用者为空、外部链接或他们的客户 ID 不存在,则它会重定向回 default.aspx 页面,以便他们可以输入他们的信息,否则它会在 customerinfo.aspx 页面上显示客户的数据。试图阻止从外部 URL 访问该页面,如果您这样做但只需要重定向到默认页面,它会显示对象引用错误。

  Uri referrer = HttpContext.Current.Request.UrlReferrer;
        if (referrer == null || string.IsNullOrEmpty(Request.UrlReferrer.ToString()) && string.IsNullOrEmpty(Session["customerID"].ToString()))
        {
//This section is skipped because it's not a null referrer.
            Response.Redirect(url: "default.aspx", endResponse: false);
            return;
        }

        if (!IsPostBack)
        {

            if (!string.IsNullOrEmpty(Request.QueryString["customerID"]))
            {
                //This section is skipped even though there's a customer ID?
                Session["customerID"] = Request.QueryString["customerID"];
                customerInfo();
            }
            else
            {

                if (string.IsNullOrEmpty(Session["customerID"].ToString()))
                {
                    //This section is skipped because it's not an empty session, there's a customer ID.
                    Response.Redirect(url: "default.aspx", endResponse: false);
                }
                else
                {
                    //This section is hit because there's a customer ID so the string isn't empty but not sure why the first isn't hit?
                    customerInfo();
                }
            }
        }

【问题讨论】:

  • 请提出一个明确的问题,因为不清楚您想要回答什么。
  • 您应该考虑到有人可以在这里伪造引荐来源网址(您将阻止“深层链接”但不一定从外部访问)...但是您在服务器上做的事情会更容易-config 级别...也许可以看到这个帖子:stackoverflow.com/questions/66437496/…
  • 我更新了@jesse-johnson 的问题。
  • 好的,我现在检查那个链接。谢谢@pcalkins。

标签: c# asp.net uri referrer


【解决方案1】:

我能够弄清楚。采用了 Albert 的部分代码并对我的代码进行了一些更改。

      Uri referrer = HttpContext.Current.Request.UrlReferrer;

      string urlName = Request.UrlReferrer.ToString(); // grabbing referring page address        
      
        if (referrer == null && urlName != "default.aspx")
        {
            Response.Redirect(url: "default.aspx", endResponse: false);
            return;
        } 

        if (!IsPostBack)
        {
            if(Session["customerID"] == null && urlName != "default.aspx") //If both are false they go to homepage
            {
                Response.Redirect(url: "default.aspx", endResponse: false);
            }
            else
            {
                customerInfo(); //or else they get the customer info on the customer page
            }
        }

【讨论】:

    【解决方案2】:

    虽然可以伪造标题 - 但它仍然可以做更多的工作。

    您可能只是不希望用户登陆某个页面,该页面显示提供了外部链接。

    所以,这将检查没有引用,即使引用是相同的

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // first page load.
    
                // user direct type in url - don't want that!
                // no referring URL at all
    
                if (Request.UrlReferrer == null)
                {
                    // user typed in URL - no referring URL
                    Response.Redirect("~/Default.aspx");
                }
    
                // user direct typed in this page, or selected from browser drop down/auto complete
                // so referring page is SAME as this page - again not from our landing page
                 if (Request.UrlReferrer.AbsoluteUri.ToString() == Request.Url.AbsoluteUri.ToString())
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
        }
    

    【讨论】:

    • 感谢 Albert,我尝试了您的实现,但没有成功。我在外部个人页面上添加了该页面的链接,它直接进入 customerinfo.aspx 页面,但没有任何数据,而不是返回默认页面?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 2011-05-14
    • 2020-06-23
    相关资源
    最近更新 更多