Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET
Tip/Trick

How to copy-protect a page using JavaScript in ASP.NET

Rate me:
Please Sign up or sign in to vote.
1.92/5 (5 votes)
20 Nov 2011CPOL 15.2K   2   5
How to protect a page from text copying using JavaScript in ASP.NET.
Quote: Many of the time we don’t want to let the user copy our page for some security reason. In my company I have to design a website for textile engineers where they can submit their article and they don’t want to let the non register user to copy their article from the page.

Here is an example of how I have done it. For this I have used JavaScript in my ASP.NET project.


Open your Project, go to your desired .aspx page, and copy the following JavaScript code in the Head section. Here I have created two types of functions to protect both from mouse and keyboard.


JavaScript
<script type="text/javascript">
    function noCopyMouse(e) {
        var isRight = (e.button) ? (e.button == 2) : (e.which == 3);

        if(isRight) {
            alert('This page is copy protected');
            return false;
        }
        return true;
    }

    function noCopyKey(e) {
        var forbiddenKeys = new Array('c','x','v');
        var keyCode = (e.keyCode) ? e.keyCode : e.which;
        var isCtrl;

        if(window.event)
            isCtrl = e.ctrlKey
        else
            isCtrl = (window.Event) ? ((e.modifiers & 
                        Event.CTRL_MASK) == Event.CTRL_MASK) : false;

        if(isCtrl) {
            for(i = 0; i < forbiddenKeys.length; i++) {
                if(forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()) {
                    alert('This page is copy protected');
                    return false;
                }
            }
        }
        return true;
    }
</script>

Now you have to select which part in your page you don’t want to copy. Here I don’t want to let copy my entire page so I use a panel and create all my tables in the panel.


XML
<asp:Panel ID="Panel1" runat="server">
       <table cellpadding="0" cellspacing="0" width="100%">
           <tr>
               <td>
                   <asp:Label ID="Label5" runat="server" 
                       Text="Try to copy me."></asp:Label>
               </td>
           </tr>
       </table>
   </asp:Panel>

Now go to your .cs page and write the following code in the Load event.


C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Panel1.Attributes.Add("onmousedown", "return noCopyMouse(event);");
        Panel1.Attributes.Add("onkeydown", "return noCopyKey(event);");
    }
}

Run your project, select some text from the page, and try to copy it, a warning message will be shown.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Grameen Communications
Bangladesh Bangladesh
I have been working in .Net platform over 4 years, I am expert in both Desktop and Web Application. I used both C# and VB for my code. I love to face challenge in coding, because I always believe that "Failure doesn't kill you... it increases your desire to make something happen.

Comments and Discussions

 
GeneralReason for my vote of 1 Ctrl Ins? Pin
Lockwood22-Nov-11 4:53
Lockwood22-Nov-11 4:53 
GeneralReason for my vote of 1 Just silly. If someone wants to copy... Pin
fjdiewornncalwe21-Nov-11 6:52
professionalfjdiewornncalwe21-Nov-11 6:52 
GeneralReason for my vote of 1 For reasons all ready given Pin
DaveAuld20-Nov-11 10:06
professionalDaveAuld20-Nov-11 10:06 
GeneralWhat if the user Saves the page and then edits it? Bang goes... Pin
DaveAuld20-Nov-11 10:06
professionalDaveAuld20-Nov-11 10:06 
GeneralReason for my vote of 1 Gives a false sense of security: doe... Pin
OriginalGriff20-Nov-11 0:23
mveOriginalGriff20-Nov-11 0:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.