Thursday, February 12, 2015

How to restrict the user to copy and paste the URL in browser address bar in ASP.NET

How to restrict the user to copy and paste the URL in browser address bar in ASP.NET

 

This is one of the common restrictions you have to apply on a web application. If user is already login to the application and user knows the page extension user can directly copy and paste the URL in web browser to open the page, because user has got already an active session.

In such cases what we have to do is, we will make use of UrlReferrer property.

Please check below code, you may write below code in the page load of each page you wanted to restrict copy and paste of URL to open the page.

string strPreviousPage = "";
 if (Request.UrlReferrer != null)
   {
    strPreviousPage = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];         
    }       
if(strPreviousPage =="")
    {
      Response.Redirect("~/Login.aspx");
     }

Above code will forcefully redirect to Login page if user copy and paste the URL to open the page.

Also along with this code you might be using your regular authentication code.

Please post your queries if you have any with this code and technique.

1 comment: