How can i change session when open url in another

2020-06-29 08:30发布

问题:

I am developing one web application in asp.net. I am opening my all pages on pop up window. I want to expire my session or change the session value when someone copy the url and paste it into another tab. How can i implement it ? Please help me out.

回答1:

A simple way would be to check Request.UrlReferrer. The Referrer would be empty if the user copy pastes a URL.

A couple of points you should consider before using this:

  1. Provide exceptions for any pages that can be directly entered by the user. For e.g. the login page or a page that can be bookmarked
  2. I believe a pop can be opened from Javascript, without a referrer. Make sure your existing code is not using this method to open a pop up.

For a generic way to determine if the user has opened a new tab, see here



回答2:

I have solved this problem by using this

   string referer = Request.ServerVariables["HTTP_REFERER"];
        if (string.IsNullOrEmpty(referer))
        {
            Response.Redirect("../Index.htm");
        }