Set validaterequest in code-behind?

A

AG

Is there any way to turn validaterequest page directive on/off via code
behind?
If so, an example or link to one would be appreciated.

I need to turn it off in cases of logged in users belonging to specific
roles.
 
B

bruce barker

as request validation is done before page processing events are called,
you can not do it in the codebehind.



-- bruce (sqlwork.com)
 
A

AG

Bruce,

Thanks for the quick response.
What about elsewhere? Like Application_BeginRequest.
 
Z

Zhi-Qiang Ni[MSFT]

Hi AG,

Usually, we set the validateRequest attribute of the Page directive to
false to disable request validation on a page:
<%@ Page validateRequest="false" %>

Or set the validateRequest attribute of the <pages /> section to false to
disable request validation for your application:
<configuration>
<system.web>
<pages validateRequest="false" />
</system.web>
</configuration>

To disable validation in a specific control, we can set the control's
CausesValidation property to false.
<asp:Button id="Button1" runat="server"
Text="Cancel" CausesValidation="False">
</asp:Button>

We can set the application's configuration from code behind but single
page. To achieve this, refer to this code.
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
' Get the Web application configuration.
Dim configuration As System.Configuration.Configuration = _

System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/")

' Get the section.
Dim pagesSection As System.Web.Configuration.PagesSection = _
CType(configuration.GetSection("system.web/pages"), _
System.Web.Configuration.PagesSection)
pagesSection.ValidateRequest = False
End Sub
Note that, we cannot change the setting neither in the Application
BeginRequest event nor in single page's code behind side.

Some related documentations:
http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessectio
n.aspx
http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessectio
n.validaterequest.aspx
http://msdn.microsoft.com/en-us/library/4c2kcht0.aspx
http://www.asp.net/learn/whitepapers/request-validation/

Workarounds when validateRequest is True:
Grant all users access to the Default1.aspx page:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q316871
Handle validateRequest error and then execute in the error page:
http://blogs.msdn.com/kaevans/archive/2003/07/07/9791.aspx
Encode HTML:
http://weblogs.asp.net/mhawley/archive/2004/03/15/89762.aspx

--
Sincerely,

Zhi-Qiang Ni

Microsoft Online Support


==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================

--------------------
| Reply-To: "AG" <[email protected]>
| From: "AG" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Set validaterequest in code-behind?
| Date: Wed, 20 Jan 2010 22:03:57 -0500
| Lines: 30
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.5843
| X-RFC2646: Format=Flowed; Response
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
| X-EsetScannerBuild: 6435
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: ool-45739eda.dyn.optonline.net 69.115.158.218
| Path: TK2MSFTNGHUB02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTNGP05.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet:95938
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Bruce,
|
| Thanks for the quick response.
| What about elsewhere? Like Application_BeginRequest.
|
| --
|
| AG
| Email: npATadhdataDOTcom
|
|
| | > as request validation is done before page processing events are called,
| > you can not do it in the codebehind.
| >
| >
| >
| > -- bruce (sqlwork.com)
| >
| > AG wrote:
| >> Is there any way to turn validaterequest page directive on/off via
code
| >> behind?
| >> If so, an example or link to one would be appreciated.
| >>
| >> I need to turn it off in cases of logged in users belonging to
specific
| >> roles.
| >>
|
|
|
|
 
A

AG

Thanks for the reply.
Changing the setting application-wide is not applicable to my situation.

So bottom line is that if I want to allow certain users to enter html, etc.
on a page, I need to create a separate page, with validaterequest=false,
that only they have permission for.

--

AG
Email: npATadhdataDOTcom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top