Thursday, March 22, 2012
Server.HTMLEncode
The following is lifted straight from the MSDN help topic for
HttpServerUtility.HtmlEncode Method [C#]
---
The following example encodes a string for transmission by HTTP. It encodes
the string named TestString, which contains the text "This is a <Test
String>.", and copies it into the string named EncodedString as
"This+is+a+%3cTest+String%3e.".
[C#]
String TestString = "This is a <Test String>.";
StringWriter writer = new StringWriter();
Server.HtmlEncode(TestString, writer);
String EncodedString = writer.ToString();
---
However, when I run it, the value of EncodedString is "This is a <Test
String>." not "This+is+a+%3cTest+String%3e."
Am I missing something? Am I not using the correct encoding method?
Any assistance gratefully received.
Best,
Mark RaeHi,
Use Server.UrlEncode for the output you are expecting.
"Mark Rae" <mark@.mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:eBm3iJ$cEHA.592@.TK2MSFTNGP11.phx.gbl...
Hi,
The following is lifted straight from the MSDN help topic for
HttpServerUtility.HtmlEncode Method [C#]
---
The following example encodes a string for transmission by HTTP. It encodes
the string named TestString, which contains the text "This is a <Test
String>.", and copies it into the string named EncodedString as
"This+is+a+%3cTest+String%3e.".
[C#]
String TestString = "This is a <Test String>.";
StringWriter writer = new StringWriter();
Server.HtmlEncode(TestString, writer);
String EncodedString = writer.ToString();
---
However, when I run it, the value of EncodedString is "This is a <Test
String>." not "This+is+a+%3cTest+String%3e."
Am I missing something? Am I not using the correct encoding method?
Any assistance gratefully received.
Best,
Mark Rae
Mark Rae wrote:
> Hi,
> The following is lifted straight from the MSDN help topic for
> HttpServerUtility.HtmlEncode Method [C#]
> ---
> The following example encodes a string for transmission by HTTP. It encode
s
> the string named TestString, which contains the text "This is a <Test
> String>.", and copies it into the string named EncodedString as
> "This+is+a+%3cTest+String%3e.".
> [C#]
> String TestString = "This is a <Test String>.";
> StringWriter writer = new StringWriter();
> Server.HtmlEncode(TestString, writer);
> String EncodedString = writer.ToString();
> ---
> However, when I run it, the value of EncodedString is "This is a <Test
> String>." not "This+is+a+%3cTest+String%3e."
> Am I missing something? Am I not using the correct encoding method?
> Any assistance gratefully received.
> Best,
> Mark Rae
>
Try Server.UrlEncode instead. HtmlEncode is generating HTML, as it
would be expected to.
-Mike
"Mike Newton" <MNewton@.Addus.com> wrote in message
news:ewkETQ$cEHA.2236@.TK2MSFTNGP10.phx.gbl...
Mike & Silva,
> Try Server.UrlEncode instead. HtmlEncode is generating HTML, as it
> would be expected to.
Thanks very much. Is the MSDN topic wrong, then...?
Server.HTMLEncode
Hey guys, im kinda struggling on this issue. I have a form that uses an html editor. My validation is set to true bc i have email, phone, and required field validations. What my task is is trying to convert the input captured in my textbox area(html editor) and convert it bc i need to keep the html code. heres my code. any help would be great. i've tried all kinds of ways to server.htmlencode it...thx
Sub Page_Load( s As Object, e As EventArgs )
If not Page.IsPostback then
'If Page.IsValid then
ThanksPnl.visible = False
FormPnl.Visible = True
TxtContent2.Text = Server.HtmlEncode(TxtContent.Text)
'End If
End If
End Sub
<tr>
<td valign="top" width="130px">Comments/Request:</td>
<td valign="top" align="left">
<asp:textbox ID="TxtContent" CausesValidation="False" width="300px" HtmlEncode="False" TextMode="Multiline" rows="10" runat="server" />
<asp:label ID="TxtContent2" CausesValidation="False" width="300px" HtmlEncode="False" TextMode="Multiline" rows="10" runat="server" />
<script language="JavaScript">
generate_wysiwyg('TxtContent');
</script></td>
</tr>
Do you want the lable "TxtContents2" to display the encoded contents of textbox? If so, I hope this one works for you
Sub Page_Load( s As Object, e As EventArgs )
If Page.IsPostback then
'If Page.IsValid then
ThanksPnl.visible = False
FormPnl.Visible = True
TxtContent2.Text = Server.HtmlEncode(TxtContent.Text)
'End If
End If
End Sub
<tr>
<td valign="top" width="130px">Comments/Request:</td>
<td valign="top" align="left">
<asp:textbox ID="TxtContent" CausesValidation="False" width="300px" HtmlEncode="False" TextMode="Multiline" rows="10" runat="server" />
<asp:label ID="TxtContent2" CausesValidation="False" width="300px" HtmlEncode="False" TextMode="Multiline" rows="10" runat="server" />
<script language="JavaScript">
generate_wysiwyg('TxtContent');
</script></td>
</tr>
Server.HTMLEncode
The following is lifted straight from the MSDN help topic for
HttpServerUtility.HtmlEncode Method [C#]
-------------------
The following example encodes a string for transmission by HTTP. It encodes
the string named TestString, which contains the text "This is a <Test
String>.", and copies it into the string named EncodedString as
"This+is+a+%3cTest+String%3e.".
[C#]
String TestString = "This is a <Test String>.";
StringWriter writer = new StringWriter();
Server.HtmlEncode(TestString, writer);
String EncodedString = writer.ToString();
-------------------
However, when I run it, the value of EncodedString is "This is a <Test
String>." not "This+is+a+%3cTest+String%3e."
Am I missing something? Am I not using the correct encoding method?
Any assistance gratefully received.
Best,
Mark RaeHi,
Use Server.UrlEncode for the output you are expecting.
"Mark Rae" <mark@.mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:eBm3iJ$cEHA.592@.TK2MSFTNGP11.phx.gbl...
Hi,
The following is lifted straight from the MSDN help topic for
HttpServerUtility.HtmlEncode Method [C#]
-------------------
The following example encodes a string for transmission by HTTP. It encodes
the string named TestString, which contains the text "This is a <Test
String>.", and copies it into the string named EncodedString as
"This+is+a+%3cTest+String%3e.".
[C#]
String TestString = "This is a <Test String>.";
StringWriter writer = new StringWriter();
Server.HtmlEncode(TestString, writer);
String EncodedString = writer.ToString();
-------------------
However, when I run it, the value of EncodedString is "This is a <Test
String>." not "This+is+a+%3cTest+String%3e."
Am I missing something? Am I not using the correct encoding method?
Any assistance gratefully received.
Best,
Mark Rae
Mark Rae wrote:
> Hi,
> The following is lifted straight from the MSDN help topic for
> HttpServerUtility.HtmlEncode Method [C#]
> -------------------
> The following example encodes a string for transmission by HTTP. It encodes
> the string named TestString, which contains the text "This is a <Test
> String>.", and copies it into the string named EncodedString as
> "This+is+a+%3cTest+String%3e.".
> [C#]
> String TestString = "This is a <Test String>.";
> StringWriter writer = new StringWriter();
> Server.HtmlEncode(TestString, writer);
> String EncodedString = writer.ToString();
> -------------------
> However, when I run it, the value of EncodedString is "This is a <Test
> String>." not "This+is+a+%3cTest+String%3e."
> Am I missing something? Am I not using the correct encoding method?
> Any assistance gratefully received.
> Best,
> Mark Rae
Try Server.UrlEncode instead. HtmlEncode is generating HTML, as it
would be expected to.
-Mike
"Mike Newton" <MNewton@.Addus.com> wrote in message
news:ewkETQ$cEHA.2236@.TK2MSFTNGP10.phx.gbl...
Mike & Silva,
> Try Server.UrlEncode instead. HtmlEncode is generating HTML, as it
> would be expected to.
Thanks very much. Is the MSDN topic wrong, then...?
Server.HtmlEncode DataBinder
'<%# DataBinder.Eval(Container.DataItem, "CAT_ID",
"pageview.aspx?tab=1&catid={0}") %
I've tried using similar to this:
<%# Server.HtmlEncode(DataBinder.Eval(Container,
"DataItem.Field1").ToString()) %
but get error message BC30455: Argument not specified for parameter '
Many thanks'<%#"/admin/managenews.aspx?action=2&id="+DataBinder.Eval(Container.DataItem,"NID")%>'
this may help u
"Fossie" <gillmcd@.ihug.co.nz> wrote in message
news:1148111001.439320.296410@.38g2000cwa.googlegro ups.com...
> Can anyone help with Server.HtmlEncode encoding this?
> '<%# DataBinder.Eval(Container.DataItem, "CAT_ID",
> "pageview.aspx?tab=1&catid={0}") %>
> I've tried using similar to this:
> <%# Server.HtmlEncode(DataBinder.Eval(Container,
> "DataItem.Field1").ToString()) %>
> but get error message BC30455: Argument not specified for parameter '
> Many thanks
Thanks - I tried the + and got this error message:
Conversion from string "pageview.aspx?tab=1&catid=1" to type 'Double'
is not valid.
'<%#"/pageview.aspx?tab=1&catid="+DataBinder.Eval(Container.DataItem,"CAT_ID")%>'
"Fossie" <gillmcd@.ihug.co.nz> wrote in message
news:1148112901.230750.47520@.j73g2000cwa.googlegro ups.com...
> Thanks - I tried the + and got this error message:
> Conversion from string "pageview.aspx?tab=1&catid=1" to type 'Double'
> is not valid.
<asp:HyperLink id="hyperlinkk1" Runat="server"
NavigateUrl='<%#"/pageview.aspx?tab=1&catid="+DataBinder.Eval(Container.DataItem,"CAT_ID")%>'</asp:HyperLink>
"Fossie" <gillmcd@.ihug.co.nz> wrote in message
news:1148112901.230750.47520@.j73g2000cwa.googlegro ups.com...
> Thanks - I tried the + and got this error message:
> Conversion from string "pageview.aspx?tab=1&catid=1" to type 'Double'
> is not valid.
Server.HTMLEncode and the # (pound) symbol
I am building a querystring and the # comes through as a #, which of
course kills the rest of the items in the querystring.
Any idea? Is there another function I can use? For now I just wrote
my own to swap out any #s.query strings should not be HTML encoded, but rather URL encoded. try
HttpUtility.UrlEncode
-- bruce (sqlwork.com)
"Chris" <cmay@.walshgroup.com> wrote in message
news:44f6e099.0407130720.5cbcd001@.posting.google.c om...
> Can someone tell me why HTMLEncode doesn't convert # to %23?
> I am building a querystring and the # comes through as a #, which of
> course kills the rest of the items in the querystring.
> Any idea? Is there another function I can use? For now I just wrote
> my own to swap out any #s.
Holy crap, I can't believe that I didn't realize what I was doing.
I knew something wasn't right... I am a disgrace :)
Thanks!
"bruce barker" <nospam_brubar@.safeco.com> wrote in message news:<#83EIDPaEHA.1152@.TK2MSFTNGP09.phx.gbl>...
> query strings should not be HTML encoded, but rather URL encoded. try
> HttpUtility.UrlEncode
> -- bruce (sqlwork.com)
>
> "Chris" <cmay@.walshgroup.com> wrote in message
> news:44f6e099.0407130720.5cbcd001@.posting.google.c om...
> > Can someone tell me why HTMLEncode doesn't convert # to %23?
> > I am building a querystring and the # comes through as a #, which of
> > course kills the rest of the items in the querystring.
> > Any idea? Is there another function I can use? For now I just wrote
> > my own to swap out any #s.
Server.HTMLEncode and the # (pound) symbol
I am building a querystring and the # comes through as a #, which of
course kills the rest of the items in the querystring.
Any idea? Is there another function I can use? For now I just wrote
my own to swap out any #s.query strings should not be HTML encoded, but rather URL encoded. try
HttpUtility.UrlEncode
-- bruce (sqlwork.com)
"Chris" <cmay@.walshgroup.com> wrote in message
news:44f6e099.0407130720.5cbcd001@.posting.google.com...
> Can someone tell me why HTMLEncode doesn't convert # to %23?
> I am building a querystring and the # comes through as a #, which of
> course kills the rest of the items in the querystring.
> Any idea? Is there another function I can use? For now I just wrote
> my own to swap out any #s.
Holy crap, I can't believe that I didn't realize what I was doing.
I knew something wasn't right... I am a disgrace :)
Thanks!
"bruce barker" <nospam_brubar@.safeco.com> wrote in message news:<#83EIDPaEHA.1152@.TK2MSFTNG
P09.phx.gbl>...
> query strings should not be HTML encoded, but rather URL encoded. try
> HttpUtility.UrlEncode
> -- bruce (sqlwork.com)
>
> "Chris" <cmay@.walshgroup.com> wrote in message
> news:44f6e099.0407130720.5cbcd001@.posting.google.com...
Server.HTMLEncode and performance
I'm dynamically loading an assembly into an ASP.NET application. In the
control that I load from that dynamic assembly I've tried to use
Server.HTMLEncode but this results in a serious performance loss. Is there
anyone who knows why or is there an alternative for server.htmlencode
method? Without the server.htmlencode the thingy works very fine.
greetz
MichaelThe HtmlEncode mathod will work as fast as the length of the string you are
encoding. If the string is short, it will be fast. If the string is long, it
will be slow.
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Michael Gaillez" <michael.gaillez@.howest.be> wrote in message
news:uS0Z52fWEHA.3120@.TK2MSFTNGP12.phx.gbl...
> Hi,
> I'm dynamically loading an assembly into an ASP.NET application. In the
> control that I load from that dynamic assembly I've tried to use
> Server.HTMLEncode but this results in a serious performance loss. Is there
> anyone who knows why or is there an alternative for server.htmlencode
> method? Without the server.htmlencode the thingy works very fine.
> greetz
> Michael
>
Ok that I can understand. But that is not the reason why I'm losing
performance in this application. If I'm doing exactly the same thing without
an dynamic assembly load it works pretty fast. If I use it with assembly
load it works very slow. So in my opinion the length doesn't really matter
in this case. But what I've found meanwhile is the httputility class were
the functionallity resides from. I will try that because then I don't have
to do this: "Page.Server...." . My guess is that this should do the trick
because reflection might not handle page.server very well.
greets
Michael
"Kevin Spencer" <kspencer@.takempis.com> wrote in message
news:uIi7lwgWEHA.716@.TK2MSFTNGP11.phx.gbl...
> The HtmlEncode mathod will work as fast as the length of the string you
are
> encoding. If the string is short, it will be fast. If the string is long,
it
> will be slow.
> --
> HTH,
> Kevin Spencer
> .Net Developer
> Microsoft MVP
> Big things are made up
> of lots of little things.
> "Michael Gaillez" <michael.gaillez@.howest.be> wrote in message
> news:uS0Z52fWEHA.3120@.TK2MSFTNGP12.phx.gbl...
there
>
The HttpUtility.HtmlEncode method will definitely work faster, as it is
static.
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Michael Gaillez" <michael.gaillez@.howest.be> wrote in message
news:ez5bJ9gWEHA.1888@.TK2MSFTNGP11.phx.gbl...
> Ok that I can understand. But that is not the reason why I'm losing
> performance in this application. If I'm doing exactly the same thing
without
> an dynamic assembly load it works pretty fast. If I use it with assembly
> load it works very slow. So in my opinion the length doesn't really matter
> in this case. But what I've found meanwhile is the httputility class were
> the functionallity resides from. I will try that because then I don't have
> to do this: "Page.Server...." . My guess is that this should do the trick
> because reflection might not handle page.server very well.
> greets
> Michael
> "Kevin Spencer" <kspencer@.takempis.com> wrote in message
> news:uIi7lwgWEHA.716@.TK2MSFTNGP11.phx.gbl...
> are
long,
> it
the
> there
>
Server.HTMLEncode and performance
I'm dynamically loading an assembly into an ASP.NET application. In the
control that I load from that dynamic assembly I've tried to use
Server.HTMLEncode but this results in a serious performance loss. Is there
anyone who knows why or is there an alternative for server.htmlencode
method? Without the server.htmlencode the thingy works very fine.
greetz
MichaelThe HtmlEncode mathod will work as fast as the length of the string you are
encoding. If the string is short, it will be fast. If the string is long, it
will be slow.
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Michael Gaillez" <michael.gaillez@.howest.be> wrote in message
news:uS0Z52fWEHA.3120@.TK2MSFTNGP12.phx.gbl...
> Hi,
> I'm dynamically loading an assembly into an ASP.NET application. In the
> control that I load from that dynamic assembly I've tried to use
> Server.HTMLEncode but this results in a serious performance loss. Is there
> anyone who knows why or is there an alternative for server.htmlencode
> method? Without the server.htmlencode the thingy works very fine.
> greetz
> Michael
Ok that I can understand. But that is not the reason why I'm losing
performance in this application. If I'm doing exactly the same thing without
an dynamic assembly load it works pretty fast. If I use it with assembly
load it works very slow. So in my opinion the length doesn't really matter
in this case. But what I've found meanwhile is the httputility class were
the functionallity resides from. I will try that because then I don't have
to do this: "Page.Server...." . My guess is that this should do the trick
because reflection might not handle page.server very well.
greets
Michael
"Kevin Spencer" <kspencer@.takempis.com> wrote in message
news:uIi7lwgWEHA.716@.TK2MSFTNGP11.phx.gbl...
> The HtmlEncode mathod will work as fast as the length of the string you
are
> encoding. If the string is short, it will be fast. If the string is long,
it
> will be slow.
> --
> HTH,
> Kevin Spencer
> .Net Developer
> Microsoft MVP
> Big things are made up
> of lots of little things.
> "Michael Gaillez" <michael.gaillez@.howest.be> wrote in message
> news:uS0Z52fWEHA.3120@.TK2MSFTNGP12.phx.gbl...
> > Hi,
> > I'm dynamically loading an assembly into an ASP.NET application. In the
> > control that I load from that dynamic assembly I've tried to use
> > Server.HTMLEncode but this results in a serious performance loss. Is
there
> > anyone who knows why or is there an alternative for server.htmlencode
> > method? Without the server.htmlencode the thingy works very fine.
> > greetz
> > Michael
The HttpUtility.HtmlEncode method will definitely work faster, as it is
static.
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Michael Gaillez" <michael.gaillez@.howest.be> wrote in message
news:ez5bJ9gWEHA.1888@.TK2MSFTNGP11.phx.gbl...
> Ok that I can understand. But that is not the reason why I'm losing
> performance in this application. If I'm doing exactly the same thing
without
> an dynamic assembly load it works pretty fast. If I use it with assembly
> load it works very slow. So in my opinion the length doesn't really matter
> in this case. But what I've found meanwhile is the httputility class were
> the functionallity resides from. I will try that because then I don't have
> to do this: "Page.Server...." . My guess is that this should do the trick
> because reflection might not handle page.server very well.
> greets
> Michael
> "Kevin Spencer" <kspencer@.takempis.com> wrote in message
> news:uIi7lwgWEHA.716@.TK2MSFTNGP11.phx.gbl...
> > The HtmlEncode mathod will work as fast as the length of the string you
> are
> > encoding. If the string is short, it will be fast. If the string is
long,
> it
> > will be slow.
> > --
> > HTH,
> > Kevin Spencer
> > .Net Developer
> > Microsoft MVP
> > Big things are made up
> > of lots of little things.
> > "Michael Gaillez" <michael.gaillez@.howest.be> wrote in message
> > news:uS0Z52fWEHA.3120@.TK2MSFTNGP12.phx.gbl...
> > > Hi,
> > > > I'm dynamically loading an assembly into an ASP.NET application. In
the
> > > control that I load from that dynamic assembly I've tried to use
> > > Server.HTMLEncode but this results in a serious performance loss. Is
> there
> > > anyone who knows why or is there an alternative for server.htmlencode
> > > method? Without the server.htmlencode the thingy works very fine.
> > > > greetz
> > > > Michael
> >
server.htmlencode method
hi to all,
As we know that the server.htmlencode method makes html encodeing so what i understand hat if i have a text like that <b>Test string</b>
then when i apply the server.htmlencode method on that text then the output will be :
<b>Test String</b>
and the output after applying this method will be displayed on a Web browser as:
<b>Test string</b>
so my question why we directly send the text <b>Test string</b> to the web browser without using the server.htmlencode method to make encoding?
so ineed some clarification on that topic.
your help is highly appreciated
Best regards
wissam1:
why we directly send the text <b>Test string</b> to the web browser without using the server.htmlencode method to make encoding?
If you do not encode, it will behave as markup (you would see a bolden string in your sample), with all security concerns eventually involved. If instead you do encode, it will render as a plain string.
That's all i guess.
HTH. -LV
HI
HtmlEncoding is done mostly to avoid XSS (Cross Site Scripting) attacks.
Google for more on that.!
Thanks
Server.HtmlEncode fails with "Server is undefined"
various sources, some of which might include dodgy characters such as
"<". Natch, IE interprets these as potentially dangerous and provides
a mechanism to encode/decode them. However I can't seem to get it to
work:
...
<asp:textbox id=txtDescription style="Z-INDEX: 102; LEFT: 111px;
POSITION: absolute; TOP: 124px" tabIndex=29 runat="server"
onblur="return ReplaceScriptCode()" Width="553px" Height="52px"
CssClass="STANDARD" Font-Names="Arial" Font-Size="8pt"
TextMode="MultiLine"></asp:textbox>
...
<script language="vb" runat=server>
Public Sub ReplaceScriptCode()
txtDescription.Text = Server.HtmlEncode(txtDescription.Text)
End Sub
</script>
The app. barfs, telling me that "Server is undefined". Thoughts?
Thanks
Edwardteddysnips@.hotmail.com wrote:
> In my application I need to allow users to cut 'n' paste stuff from
> various sources, some of which might include dodgy characters such as
> "<". Natch, IE interprets these as potentially dangerous and provides
> a mechanism to encode/decode them. However I can't seem to get it to
> work:
> ...
> <asp:textbox id=txtDescription style="Z-INDEX: 102; LEFT: 111px;
> POSITION: absolute; TOP: 124px" tabIndex=29 runat="server"
> onblur="return ReplaceScriptCode()" Width="553px" Height="52px"
> CssClass="STANDARD" Font-Names="Arial" Font-Size="8pt"
> TextMode="MultiLine"></asp:textbox>
> ...
> <script language="vb" runat=server>
> Public Sub ReplaceScriptCode()
> txtDescription.Text = Server.HtmlEncode(txtDescription.Text)
> End Sub
> </script>
> The app. barfs, telling me that "Server is undefined". Thoughts?
> Thanks
> Edward
>
You are mixing server code and client code. The code in the onblur event
doesn't call the VB subroutine that you have put in the server code, it
calls the client side function with the same name.
As you don't get the error message that ReplaceScriptCode is undefined,
I assume that you also have a Javascript or VBScript function with that
name?
If you have put the same code in that function as in your VB server side
subroutine, that will obviously not work, as there is no Server object
on the client side.
What is it that you are trying to do, really? There is nothing dangerous
about pasting markup code into a textbox. ASP.NET won't let you post
stuff that contains markup code to the server by default, but that is a
completely different thing, and has a completely different solution.
Gran Andersson
_____
http://www.guffa.com
Edward
I think your problem is that the OnBlur event, which is calling your
ReplaceScriptCode is a client side event, whereas the Server object is serve
r
side. The code works fine if executed from from code behind:
<asp:TextBox ID="txtDescription" Style="z-index: 102; left:
111px; position: absolute;
top: 124px" TabIndex="29" runat="server" onblur="return
ReplaceScriptCode()"
Width="553px" Height="52px" CssClass="STANDARD"
Font-Names="Arial" Font-Size="8pt"
TextMode="MultiLine"></asp:TextBox>
<script language="vb" runat="server">
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
txtDescription.Text =
Server.HtmlEncode(txtDescription.Text)
End Sub
</script>
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
Just hit the button to test it.
Hope this helps
Tom
"teddysnips@.hotmail.com" wrote:
> In my application I need to allow users to cut 'n' paste stuff from
> various sources, some of which might include dodgy characters such as
> "<". Natch, IE interprets these as potentially dangerous and provides
> a mechanism to encode/decode them. However I can't seem to get it to
> work:
> ...
> <asp:textbox id=txtDescription style="Z-INDEX: 102; LEFT: 111px;
> POSITION: absolute; TOP: 124px" tabIndex=29 runat="server"
> onblur="return ReplaceScriptCode()" Width="553px" Height="52px"
> CssClass="STANDARD" Font-Names="Arial" Font-Size="8pt"
> TextMode="MultiLine"></asp:textbox>
> ...
> <script language="vb" runat=server>
> Public Sub ReplaceScriptCode()
> txtDescription.Text = Server.HtmlEncode(txtDescription.Text)
> End Sub
> </script>
> The app. barfs, telling me that "Server is undefined". Thoughts?
> Thanks
> Edward
>
On 4 Jul, 12:09, G=F6ran Andersson <g...@.guffa.com> wrote:
> teddysn...@.hotmail.com wrote:
[=2E..]
> What is it that you are trying to do, really? There is nothing dangerous
> about pasting markup code into a textbox. ASP.NET won't let you post
> stuff that contains markup code to the server by default, but that is a
> completely different thing, and has a completely different solution.
As you say. The problem comes when the user tries to post the text
(dumb of me, I know, I should have mentioned that).
The user wants to be able to paste stuff into text boxes which is then
sent to the server for processing (generally, storage in a database
and later retrieval).
I don't want to remove validation at page level - in fact I doubt the
clients would let me. I rolled my own client-side jscript to strip
out markup code which was called from the OnBlur event, but I thought
I could improve on it using built-in functions.
In short, the requirement is:
ASP.NET with VB code behind.
User pastes stuff into an ASP TextBox
Presses "Save" button. Save button is asp:Button running server-side.
Remove/replace "dangerous" code with HtmlEncoded stuff and save to
database.
Thoughts?
Thanks,
Edward
teddysnips@.hotmail.com wrote:
> On 4 Jul, 12:09, Gran Andersson <g...@.guffa.com> wrote:
> [...]
> As you say. The problem comes when the user tries to post the text
> (dumb of me, I know, I should have mentioned that).
> The user wants to be able to paste stuff into text boxes which is then
> sent to the server for processing (generally, storage in a database
> and later retrieval).
> I don't want to remove validation at page level - in fact I doubt the
> clients would let me.
That validation is not required, as long as you make sure that you treat
the data as unvalidated on the server side. That means that you never
put it on a page or use it in a database query without making sure that
it's properly encoded.
As the validation doesn't cover any possible circumstances, that's what
you already should be doing anyway.
Read up on cross site scripting and sql injections, so that you know how
these types of security leaks can be exploited.
> I rolled my own client-side jscript to strip
> out markup code which was called from the OnBlur event, but I thought
> I could improve on it using built-in functions.
> In short, the requirement is:
> ASP.NET with VB code behind.
> User pastes stuff into an ASP TextBox
> Presses "Save" button. Save button is asp:Button running server-side.
> Remove/replace "dangerous" code with HtmlEncoded stuff and save to
> database.
> Thoughts?
Storing it html encoded in the database served no purpose. Html code can
not harm the database. You just have to make sure it isn't put on a page
without being encoded.
Gran Andersson
_____
http://www.guffa.com
On 4 Jul, 15:58, G=F6ran Andersson <g...@.guffa.com> wrote:
> teddysn...@.hotmail.com wrote:
us
[=2E..]
Thanks for all your help so far - it really is appreciated.
I reaslise that there's no danger to the database from the text, or
markup code (actually, in the scenario at present, it's simply users
copying and pasting e-mail messages wholesale, including the reply
prefixes e.g. >> as seen above which are the main problem). I just
want to allow the users to copy and paste what they want and save this
text to the database without incurring "A potentially dangerous
Request.Form value ... " error. So it seems to make sense to htlm-
encode the text, whatever it is, save it, and then un-html-encode it
when the data is retrieved and displayed.
I can't be the only person in history to have encountered this problem
yet I can't seem to find any examples that satisfy my requirements. I
thought this page:
http://www.asp.net/faq/RequestValidation.aspx#5
had it, but I can't make it work.
Any further ideas before you lose patience completely?
Edward
<teddysnips@.hotmail.com> wrote in message
news:1183563744.577845.215450@.m36g2000hse.googlegroups.com...
> Any further ideas before you lose patience completely?
Would something like this solve your problem:?
http://scriptasylum.com/tutorials/e...ode-decode.html
Mark Rae
ASP.NET MVP
http://www.markrae.net
teddysnips@.hotmail.com wrote:
> On 4 Jul, 15:58, Gran Andersson <g...@.guffa.com> wrote:
> [...]
> Thanks for all your help so far - it really is appreciated.
> I reaslise that there's no danger to the database from the text, or
> markup code (actually, in the scenario at present, it's simply users
> copying and pasting e-mail messages wholesale, including the reply
> prefixes e.g. >> as seen above which are the main problem). I just
> want to allow the users to copy and paste what they want and save this
> text to the database without incurring "A potentially dangerous
> Request.Form value ... " error. So it seems to make sense to htlm-
> encode the text, whatever it is, save it, and then un-html-encode it
> when the data is retrieved and displayed.
No, that doesn't make sense. Encoding only makes sense if you use an
encoding that is relevant for where you put the data. Html-encoding data
that goes into the database and then decode it when you read it is a
total waste of time, space and code. It serves no purpose at all.
Also, as you were trying to encode the text on the client side, that
doesn't make it any safer. On the contrary, as you would later decode
this text that you can't safely say that you know is properly encoded,
you would instead open up the security hole that the validation is meant
to help prevent.
> I can't be the only person in history to have encountered this problem
Of course not. I have handled the problem myself several times.
> yet I can't seem to find any examples that satisfy my requirements. I
> thought this page:
> http://www.asp.net/faq/RequestValidation.aspx#5
> had it, but I can't make it work.
Just pick that single property from that page and put in your @.Page
directive. That turns the validation off.
Then you just handle the data as normal, just store it in the database
at it is. When you later put it on a page, make sure that it's properly
html encoded.
Gran Andersson
_____
http://www.guffa.com
Server.HtmlEncode fails with "Server is undefined"
various sources, some of which might include dodgy characters such as
"<". Natch, IE interprets these as potentially dangerous and provides
a mechanism to encode/decode them. However I can't seem to get it to
work:
...
<asp:textbox id=txtDescription style="Z-INDEX: 102; LEFT: 111px;
POSITION: absolute; TOP: 124px" tabIndex=29 runat="server"
onblur="return ReplaceScriptCode()" Width="553px" Height="52px"
CssClass="STANDARD" Font-Names="Arial" Font-Size="8pt"
TextMode="MultiLine"></asp:textbox>
...
<script language="vb" runat=server>
Public Sub ReplaceScriptCode()
txtDescription.Text = Server.HtmlEncode(txtDescription.Text)
End Sub
</script>
The app. barfs, telling me that "Server is undefined". Thoughts?
Thanks
Edwardteddysnips@.hotmail.com wrote:
Quote:
Originally Posted by
In my application I need to allow users to cut 'n' paste stuff from
various sources, some of which might include dodgy characters such as
"<". Natch, IE interprets these as potentially dangerous and provides
a mechanism to encode/decode them. However I can't seem to get it to
work:
>
...
<asp:textbox id=txtDescription style="Z-INDEX: 102; LEFT: 111px;
POSITION: absolute; TOP: 124px" tabIndex=29 runat="server"
onblur="return ReplaceScriptCode()" Width="553px" Height="52px"
CssClass="STANDARD" Font-Names="Arial" Font-Size="8pt"
TextMode="MultiLine"></asp:textbox>
>
...
>
<script language="vb" runat=server>
Public Sub ReplaceScriptCode()
txtDescription.Text = Server.HtmlEncode(txtDescription.Text)
End Sub
</script>
>
The app. barfs, telling me that "Server is undefined". Thoughts?
>
Thanks
>
Edward
>
You are mixing server code and client code. The code in the onblur event
doesn't call the VB subroutine that you have put in the server code, it
calls the client side function with the same name.
As you don't get the error message that ReplaceScriptCode is undefined,
I assume that you also have a Javascript or VBScript function with that
name?
If you have put the same code in that function as in your VB server side
subroutine, that will obviously not work, as there is no Server object
on the client side.
What is it that you are trying to do, really? There is nothing dangerous
about pasting markup code into a textbox. ASP.NET won't let you post
stuff that contains markup code to the server by default, but that is a
completely different thing, and has a completely different solution.
--
Gran Andersson
_____
http://www.guffa.com
Edward
I think your problem is that the OnBlur event, which is calling your
ReplaceScriptCode is a client side event, whereas the Server object is server
side. The code works fine if executed from from code behind:
<asp:TextBox ID="txtDescription" Style="z-index: 102; left:
111px; position: absolute;
top: 124px" TabIndex="29" runat="server" onblur="return
ReplaceScriptCode()"
Width="553px" Height="52px" CssClass="STANDARD"
Font-Names="Arial" Font-Size="8pt"
TextMode="MultiLine"></asp:TextBox>
<script language="vb" runat="server">
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
txtDescription.Text =
Server.HtmlEncode(txtDescription.Text)
End Sub
</script>
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
Just hit the button to test it.
Hope this helps
Tom
"teddysnips@.hotmail.com" wrote:
Quote:
Originally Posted by
In my application I need to allow users to cut 'n' paste stuff from
various sources, some of which might include dodgy characters such as
"<". Natch, IE interprets these as potentially dangerous and provides
a mechanism to encode/decode them. However I can't seem to get it to
work:
>
...
<asp:textbox id=txtDescription style="Z-INDEX: 102; LEFT: 111px;
POSITION: absolute; TOP: 124px" tabIndex=29 runat="server"
onblur="return ReplaceScriptCode()" Width="553px" Height="52px"
CssClass="STANDARD" Font-Names="Arial" Font-Size="8pt"
TextMode="MultiLine"></asp:textbox>
>
...
>
<script language="vb" runat=server>
Public Sub ReplaceScriptCode()
txtDescription.Text = Server.HtmlEncode(txtDescription.Text)
End Sub
</script>
>
The app. barfs, telling me that "Server is undefined". Thoughts?
>
Thanks
>
Edward
>
>
On 4 Jul, 12:09, Gran Andersson <g...@.guffa.comwrote:
Quote:
Originally Posted by
teddysn...@.hotmail.com wrote:
[...]
Quote:
Originally Posted by
What is it that you are trying to do, really? There is nothing dangerous
about pasting markup code into a textbox. ASP.NET won't let you post
stuff that contains markup code to the server by default, but that is a
completely different thing, and has a completely different solution.
As you say. The problem comes when the user tries to post the text
(dumb of me, I know, I should have mentioned that).
The user wants to be able to paste stuff into text boxes which is then
sent to the server for processing (generally, storage in a database
and later retrieval).
I don't want to remove validation at page level - in fact I doubt the
clients would let me. I rolled my own client-side jscript to strip
out markup code which was called from the OnBlur event, but I thought
I could improve on it using built-in functions.
In short, the requirement is:
ASP.NET with VB code behind.
User pastes stuff into an ASP TextBox
Presses "Save" button. Save button is asp:Button running server-side.
Remove/replace "dangerous" code with HtmlEncoded stuff and save to
database.
Thoughts?
Thanks,
Edward
On 4 Jul, 15:58, Gran Andersson <g...@.guffa.comwrote:
Quote:
Originally Posted by
teddysn...@.hotmail.com wrote:
Quote:
Originally Posted by
On 4 Jul, 12:09, Gran Andersson <g...@.guffa.comwrote:
Quote:
Originally Posted by
teddysn...@.hotmail.com wrote:
[...]
Quote:
Originally Posted by
What is it that you are trying to do, really? There is nothing dangerous
about pasting markup code into a textbox. ASP.NET won't let you post
stuff that contains markup code to the server by default, but that is a
completely different thing, and has a completely different solution.
[...]
Thanks for all your help so far - it really is appreciated.
I reaslise that there's no danger to the database from the text, or
markup code (actually, in the scenario at present, it's simply users
copying and pasting e-mail messages wholesale, including the reply
prefixes e.g. >as seen above which are the main problem). I just
want to allow the users to copy and paste what they want and save this
text to the database without incurring "A potentially dangerous
Request.Form value ... " error. So it seems to make sense to htlm-
encode the text, whatever it is, save it, and then un-html-encode it
when the data is retrieved and displayed.
I can't be the only person in history to have encountered this problem
yet I can't seem to find any examples that satisfy my requirements. I
thought this page:
http://www.asp.net/faq/RequestValidation.aspx#5
had it, but I can't make it work.
Any further ideas before you lose patience completely?
Edward
<teddysnips@.hotmail.comwrote in message
news:1183563744.577845.215450@.m36g2000hse.googlegr oups.com...
Quote:
Originally Posted by
Any further ideas before you lose patience completely?
Would something like this solve your problem:?
http://scriptasylum.com/tutorials/e...ode-decode.html
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
teddysnips@.hotmail.com wrote:
Quote:
Originally Posted by
On 4 Jul, 15:58, Gran Andersson <g...@.guffa.comwrote:
Quote:
Originally Posted by
>teddysn...@.hotmail.com wrote:
Quote:
Originally Posted by
>>On 4 Jul, 12:09, Gran Andersson <g...@.guffa.comwrote:
>>>teddysn...@.hotmail.com wrote:
>>[...]
>>>What is it that you are trying to do, really? There is nothing dangerous
>>>about pasting markup code into a textbox. ASP.NET won't let you post
>>>stuff that contains markup code to the server by default, but that is a
>>>completely different thing, and has a completely different solution.
[...]
>
Thanks for all your help so far - it really is appreciated.
>
I reaslise that there's no danger to the database from the text, or
markup code (actually, in the scenario at present, it's simply users
copying and pasting e-mail messages wholesale, including the reply
prefixes e.g. >as seen above which are the main problem). I just
want to allow the users to copy and paste what they want and save this
text to the database without incurring "A potentially dangerous
Request.Form value ... " error. So it seems to make sense to htlm-
encode the text, whatever it is, save it, and then un-html-encode it
when the data is retrieved and displayed.
No, that doesn't make sense. Encoding only makes sense if you use an
encoding that is relevant for where you put the data. Html-encoding data
that goes into the database and then decode it when you read it is a
total waste of time, space and code. It serves no purpose at all.
Also, as you were trying to encode the text on the client side, that
doesn't make it any safer. On the contrary, as you would later decode
this text that you can't safely say that you know is properly encoded,
you would instead open up the security hole that the validation is meant
to help prevent.
Quote:
Originally Posted by
I can't be the only person in history to have encountered this problem
Of course not. I have handled the problem myself several times.
Quote:
Originally Posted by
yet I can't seem to find any examples that satisfy my requirements. I
thought this page:
>
http://www.asp.net/faq/RequestValidation.aspx#5
>
had it, but I can't make it work.
Just pick that single property from that page and put in your @.Page
directive. That turns the validation off.
Then you just handle the data as normal, just store it in the database
at it is. When you later put it on a page, make sure that it's properly
html encoded.
--
Gran Andersson
_____
http://www.guffa.com
Server.HtmlEncode DataBinder
'<%# DataBinder.Eval(Container.DataItem, "CAT_ID",
"pageview.aspx?tab=1&catid={0}") %>
I've tried using similar to this:
<%# Server.HtmlEncode(DataBinder.Eval(Container,
"DataItem.Field1").ToString()) %>
but get error message BC30455: Argument not specified for parameter '
Many thanks'<%#"/admin/managenews.aspx?action=2&id="+DataBinder.Eval(Container.DataItem
,"NID")%>'
this may help u
"Fossie" <gillmcd@.ihug.co.nz> wrote in message
news:1148111001.439320.296410@.38g2000cwa.googlegroups.com...
> Can anyone help with Server.HtmlEncode encoding this?
> '<%# DataBinder.Eval(Container.DataItem, "CAT_ID",
> "pageview.aspx?tab=1&catid={0}") %>
> I've tried using similar to this:
> <%# Server.HtmlEncode(DataBinder.Eval(Container,
> "DataItem.Field1").ToString()) %>
> but get error message BC30455: Argument not specified for parameter '
> Many thanks
>
Server.HtmlEncode DataBinder
Conversion from string "pageview.aspx?tab=1&catid=1" to type 'Double'
is not valid.'<%#"/pageview.aspx?tab=1&catid="+DataBinder.Eval(Container.DataItem,"CAT_ID
")%>'
"Fossie" <gillmcd@.ihug.co.nz> wrote in message
news:1148112901.230750.47520@.j73g2000cwa.googlegroups.com...
> Thanks - I tried the + and got this error message:
> Conversion from string "pageview.aspx?tab=1&catid=1" to type 'Double'
> is not valid.
>
<asp:HyperLink id="hyperlinkk1" Runat="server"
NavigateUrl='<%#"/pageview.aspx?tab=1&catid="+DataBinder.Eval(Container.Data
Item,"CAT_ID")%>'</asp:HyperLink>
"Fossie" <gillmcd@.ihug.co.nz> wrote in message
news:1148112901.230750.47520@.j73g2000cwa.googlegroups.com...
> Thanks - I tried the + and got this error message:
> Conversion from string "pageview.aspx?tab=1&catid=1" to type 'Double'
> is not valid.
>
Server.HTMLEncode/Decode and Databinding
bleroy, maybe this is his reason:
Use Server.HTMLEncode for strings stored in a database
If your ASP pages display strings stored in database fields, you should always process the strings with the Server.HTMLEncode method, otherwise the string won't be displayed correctly in the user's browser if it contains characters that have a special meaning to HTML, such as the quote ("), the less-than (<) and greater-than (>) symbols, the ampersand symbol (&), and any character whose ANSI code is larger than 127.
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q253119
recommends using Server.HTMLEncode from forms and querystrings:
HOWTO: Review ASP Code for CSSI Vulnerability
This article describes how to identify and correct Active Server Pages (ASP) applications that are susceptible to Cross-Site Scripting Security Issues (CSSI). Only input that is not correctly validated or formatted makes your application vulnerable to attack.
You both are beside the point here.
Encoding before sending TO the database and decoding what comes FROM the database is absolutely useless. The database won't suffer from any javascript injections, believe me. Sql injections, perhaps, but X-site scripting, no way.
These papers explain how you should encode any data FROM THE USER TO THE PAGE.
So if you're storing user data to the database, you may encode it at this point or when fetching it back from the database, but don't DECODE what comes from the database before sending it to the client.
It's the "and of course [?] I use Server.HTMLDecode before displaying it or presenting the data back to the user for editing" from the original post that is utterly wrong.
> Encoding before sending TO the database and decoding what comes FROM the database is absolutely useless.
> These papers explain how you should encode any data FROM THE USER TO THE PAGE.
What is stored in the database (from the user) will be displayed on the page. Is is still wrong?
Server.HtmlEncode(createText.Text) vs. HttpUtility.HtmlEncode(createText.Text);
What is the difference? and which one should I use?
Server.HtmlEncode(createText.Text)
vs.
HttpUtility.HtmlEncode(createText.Text)
Server.HtmlEncode calls HttpUtility.HtmlEncode.
According toReflector, Server is an instance ofHttpServerUtility
Public ReadOnly Property Server As HttpServerUtility
Get
If (Me._server Is Nothing) Then
Me._server = New HttpServerUtility(Me)
End If
Return Me._server
End Get
End Property
And as noted HttpServerUtility.HtmlEncode makes a straight call to the shared method HttpUtility.HtmlEncode
Public Sub HtmlEncode(ByVal s As String, ByVal output As TextWriter)
HttpUtility.HtmlEncode(s, output)
End Sub
So for best performance, you could call directly to HttpUtility.HtmlEncode yourself.
Server.HtmlEncode(createText.Text) vs. HttpUtility.HtmlEncode(createText.Text)
From thedocumentation:
Server.HtmlEncode is a convenient way to access theSystem.Web.HttpUtility.HtmlEncode method at run time from an ASP.NET Web application. Internally,HtmlEncode usesSystem.Web.HttpUtility.HtmlEncode to encode strings.