I am confused between ASP.NET Request validation and server-side validation.
If we set ValidateRequest=”false”, as follows.
<%@ Language="C#" ValidateRequest="false" %>
<html>
<script runat="server">
void btnSubmit_Click(Object sender, EventArgs e)
{
// If ValidateRequest is false, then 'hello' is displayed
// If ValidateRequest is true, then ASP.NET returns an exception
Response.Write(txtString.Text);
}
</script>
<body>
<form id="form1" runat="server">
<asp:TextBox id="txtString" runat="server"
Text="<script>alert('hello');</script>" />
<asp:Button id="btnSubmit" runat="server" OnClick="btnSubmit_Click"
Text="Submit" />
</form>
</body>
</html>
Then can we use server-side validation such as RequiredFieldValidator Control?
Are they different things? But they both have validate key words.
Thanks for explaining them.
Yes, they are different.
ValidateRequestcauses an error to be thrown and aborts the request if any of the submitted form field values contain “dangerous” values, such as that script tag you have there, to prevent script injection attacks.The validator controls such as
RequiredFieldValidatordo not stop page processing if they fail. They are to help you process your rules and typically have nothing to do with protecting the server or application from attack.