tags:

views:

85

answers:

5

Is there any property or any function is giving which is prevent above issue..

i made a one register form.. and my friend inserted tag into username.. and it will raise a error at server side.. i tried to prevent it. with validation but.. it cant..

i did it.. check out this line..

txt_countryname.Text = HttpUtility.HtmlEncode(txt_countryname.Text);

but its not working ... – Sikender 0 secs ago [delete this comment]

help me...

+1  A: 

Server.HtmlEncode

Andrei Drynov
i did it.. check out this line.. txt_countryname.Text = HttpUtility.HtmlEncode(txt_countryname.Text); but its not working ...
Sikender
+1  A: 

I would take a look at HttpServerUtility.HtmlEncode.

Andy West
i did it.. check out this line..txt_countryname.Text = HttpUtility.HtmlEncode(txt_countryname.Text);but its not working ...
Sikender
Please specify the error you're encountering.
Andy West
+2  A: 

There is a good reason for this behavior - to avoid cross site scripting attacks.

It can be disabled by adding this to your web.config:

<configuration>
    <system.web>  
        <pages validateRequest="false" />
    </system.web> 
</configuration>

Read through this article to find out why disabling request validation is a bad idea.

As CodeMonkey noted, you can also do this on a single page basis in the @page directive:

<%@ Page validateRequest="false" %>

Which would be a better approach, by not exposing all of your pages to xss.

Oded
tell .. me if i dont false it this property.. then error will raised every time.. ok.. so..is it ok!!!! or i should put this property false.. can you tell me...
Sikender
Most importantly, if request validation is turned off, it's a good idea to make sure you do your own validation to limit security risks.
Andy West
I will vote against this approach because it disables validation on *all* pages, as opposed to my suggestion where you want to only disable validation for single pages.
CodeMonkey
ya you are right..man..
Sikender
@CodeMonkey - added your refrain. Reduce the attack surface by only disabling a single page.
Oded
Request Validation is worthless. It won't protect you from all possible attacks if you're vulnerable, and it will stop people using perfectly valid strings which look like tags when you're *not* vulnerable. Turn it off, get rid of it, burn it, find the programmers responsible and prosecute them for Criminally Bad Idea.
bobince
+2  A: 

you also can use asp:RegularExpressionValidator Example:

<asp:TextBox ID="txt_username" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Text="Error" ErrorMessage="some error message" ValidationExpression="^[a-zA-Z0-9]+[a-zA-Z0-9]{4,15}$" SetFocusOnError="True" ControlToValidate="txt_username">
pepelucaz
this term called scripting injection .. i dont. know. but we can not solved by using.. validators.. thanks.. but.. you should read oded's link article. ok..thanks..
Sikender
+2  A: 

You will want to, for that page to disable Script-validation. The reason why you are getting that error is because ASP.NET by default validates incoming posts.

Read more about it

CodeMonkey