10.02.2014 Views

Beginning Ajax With ASP.NET (2006).pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2<br />

Security Concerns When Getting Text Input<br />

Because the name is written out exactly as it was entered, you want to be very careful when accepting<br />

any assorted text from the user because exactly what someone types in is presented on the screen,<br />

including any additional HTML or JavaScript. Try putting this in when prompted:<br />

location.href=”http://www.wrox.com”;<br />

When the page is rendered, the included script runs as part of the page and redirects the browser to the<br />

Wrox web site! Although we love the Wrox web site, this is probably not at all what you intended when<br />

you asked for the user’s name. So, taking this example further, what if you blindly stored the text from<br />

the user in a database and then on the main page of your site presented a dynamic list of which users are<br />

currently online? When that user is online, suddenly anyone who visits your site is redirected to another<br />

page. This kind of attack is called HTML injection and is related to Cross Site Scripting (XSS). So to avoid<br />

this problem, you need to include code to filter out unintentional HTML elements and JavaScript.<br />

Conveniently, this kind of protection is intrinsic in Microsoft’s <strong>ASP</strong>.<strong>NET</strong> environment when using the<br />

.<strong>NET</strong> Framework 1.1 or later.<br />

Canceling the Normal Outcome of an Event<br />

So, now you can look at another event handler called onclick. Every visible element found on an<br />

HTML page provides this event handler, even the element. So, this really could be placed anywhere.<br />

But people are more accustomed to clicking hyperlinks, so for this example you are going to<br />

include this handler on an element that is used to navigate to other pages. What if you wanted to<br />

confirm that people really wanted to navigate to another page when they clicked a link? Add this HTML<br />

at the end of your current content to see how this is done:<br />

<br />

Knowledge Base Software<br />

The confirm() function presents a pop-up with both OK and Cancel buttons, as shown in Figure 2-4.<br />

When a choice is made by the user, confirm() will equal true if the user clicks OK, or false if the user<br />

clicks Cancel or closes the pop-up. Returning this true or false in an onclick event handler determines<br />

if the handling of that event should continue or not.<br />

18<br />

Figure 2-4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!