Extension Methods for AntiXss

Playing around with some C# 3.0 language features, I came up with something which is quite useful if you are doing a lot of web in/output encoding.

The following extension methods wrap the AntiXss library:

public static class Extensions


    public static string UrlEncode(this string input)


        return AntiXss.UrlEncode(input);



    public static string HtmlEncode(this string input)


        return AntiXss.HtmlEncode(input);



    // rest omitted


This allows doing something like this:

string input = “<h1>leastprivilege rocks</h1>”;


AntiXssExtensions.zip (17.19 KB)


This entry was posted in ASP.NET. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s