German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


How to Create a Wiki Like Secure Page in ASP .NET 2.0

Written by omerkamal on Jan 02, 2007
Article about secure Pages in ASP .NET 2.0

Introducation:

We are living in a Globel Village. Knowledge have no boundries. This is why the Wiki generation started. This way many people generate and share the informations.

Explanation:

This article will not talk about how to create a complete system of the wiki. Rather, We will discuss how to make a secure page in a wiki application with help of ASP .NET 2.0.

The basic entity of a Page is a Text. How it would be possible for us to create Text in a secure way? What is insecure about entring or saving a Text?

There are cases when some poople have good programming skills and enough time. They can even distroy our sysetem.  Why they do this? We won't discuss in here, but we will try to tackle the risks.

What ever a user enters is displayed in the browser. There is possible that some one enter some code instead of what he was suppose to share.

In HTML all codes are Rapped in block . If it is written the same way on the page then page reads it like some code block. will show a popup window with greeting your all users with a nice "Hi". Which actually, you did not intend to do. Similraly, some code would be even able to close the pages itself or some code even can distroy your DataBase system.

How to secure your DataBase we will put it to another discussion but let us see what magic can save us from above bugger.

 

When you saving the Text to you DataBase, do like this;

string strMessage=Context.Server.HtmlEncode(TextBoxMessage.Text);

 Now, you can pass "strMessage" to your Fucntion which save "Message" to the DataBase. This Above code will change the   into    <script> alert("hi")</script>

 

When we want to display the Code back to the page then we will do like this;

<asp:Label ID="LabelMessage" runat="server" Text='<%# Context.Server.HtmlDecode(Eval("Message").ToString())%>'> asp:Label>

 Here, Eval("Message") is our DataTable Field.

 

There could be a Different senario. May be you dont want to save the "Message" to a DataBase but you want to write it  directly to the page. Then we have to define a Function which will take care of decoding. Define a function in the codebehind.

string decodeMessage(){
LabelMessage.Text=Context.Server.HtmlDecode(TextBoxMessage.Text);
}

The above function executes when demanded. We can access the same method in our .aspx like this;

<asp:Label ID="LabelMessage" runat="server" Text='<%# Context.Server.HtmlDecode(Eval("Message").ToString())%>'> asp:Label>

Now, for example you want to change the usual tags to normal. We will define a static function. This way it will be easy for us to access it from the .aspx embeded code.

publicstatic string DecodeTags(string encodedHTML)
{
    StringBuilder strbuilder = new StringBuilder(encodedHTML); 

    strbuilder.Replace("<b>", "");
    strbuilder.Replace("</b>", "
");
    strbuilder.Replace("<i>", "");
    strbuilder.Replace("</i>", "
");
    strbuilder.Replace("<p>", "

");
    strbuilder.Replace("</p>", "

");
    strbuilder.Replace("<u>", "");
    strbuilder.Replace("</u>", "
");
    strbuilder.Replace("<br>", "
"
);
    strbuilder.Replace("</br>", "
"
);
    strbuilder.Replace("<br/>", "
"
);

    return strbuilder.ToString();
}

 This code will replace our encoded HTML tags to a readable code. To access the function from .aspx just access like other static functions. So, the good thing about Static functions is we access it with-out initialising ( i.e we dont need to create new object).

Visitors/Readers Comments
(for questions please use The Forum)



ryan

why not instead:

strbuilder.Replace("<",<");
strbuilder.Replace(">",>");

07/02/2007 11:41:54 UTC

Kamal

That is because may be user entered some code sample which contained "< "or ">". When you replace "<" or ">" like your given code then it will replace all of its instances. If you can guarantee that input data won't contain such characters then go for it.

08/02/2007 10:53:27 UTC

Pete
 Cools

18/04/2007 11:03:48 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)