Browsing articles in "Freebies"

Tip: How to change WSDL URL from internal machine name to public in WCF service?

sep 30, 2011 22:38:57   //   Geert Van Huychem   //   Freebies   //   0 reacties

Sometimes when you use wsdl.exe or "Add service reference" in VS, you get a bunch of errors, because the namespaces in the service are all referencing to localhost.

How to avoid this? Fairly easy. Just add the following lines to your config:

                                                    
                                                       
                                                         
                                                         
                    					                     
                        				                        
                            					                    
                        				                        
                     					                    
                                                          
                                                       
                                                    
                                                

Tip: Reset Windows 2000 Administrator Password

sep 30, 2011 22:38:56   //   Geert Van Huychem   //   Freebies   //   0 reacties

Forgot your Windows admin password?

Download ntpasswd here

  • This is a utility to reset the password of any user that has a valid local account on your Windows system.
  • Supports all Windows from NT3.5 to Win7, also 64 bit and also the Server versions (like 2003 and 2008)
  • You do not need to know the old password to set a new one.
  • It works offline, that is, you have to shutdown your computer and boot off a CD or USB disk to do the password reset.
  • Will detect and offer to unlock locked or disabled out user accounts!
  • There is also a registry editor and other registry utilities that works under linux/unix, and can be used for other things than password editing.

Tip: Remove namespaces using XDocument and LINQ

sep 30, 2011 22:38:56   //   Geert Van Huychem   //   Freebies   //   0 reacties

Ever crossed the point where your XPath expressions just didn't work, no matter what you tried. Chances are big that's because there are namespaces used in the xml

Below you'll find a simple way to remove them, using LINQ. Pay attention to the highlighted line. If you don't do this, all attributes will be removed.

                                                    using System.Linq;
                                                    using System.Xml.Linq;
            
                                                    namespace iFrameWorx.Core.UnitTests.Extensions
                                                    {
                                                        public static class XElementExtensions
                                                        {
                                                            public static XElement RemoveNamespaces(this XElement root)
                                                            {
                                                                var res = new XElement(
                                                                    root.Name.LocalName,
                                                                    root.HasElements ?
                                                                                         root.Elements().Select(RemoveNamespaces) :
                                                                                                                                      (object)root.Value
                                                                    );
            
                                                                res.ReplaceAttributes(
                                                                    root.Attributes().Where(attr => (!attr.IsNamespaceDeclaration)));
            
                                                                return res;
                                                            }
                                                        }
                                                    }
                                                
    
                                                using System.Linq;
                                                using System.Xml.Linq;
            
                                                using NUnit.Framework;
            
                                                namespace iFrameWorx.Core.UnitTests.Extensions
                                                {
                                                    [TestFixture]
                                                    public class RemoveNamespacesUnitTest
                                                    {
                                                        [Test]
                                                        public void RemoveNameSpaces()
                                                        {
                                                            var xDocumentOriginal = XDocument.Load("../../624081.xml", LoadOptions.None);
                                                            Assert.IsNotNull(xDocumentOriginal);
                                                            Assert.IsNotNull(xDocumentOriginal.Root);
            
                                                            Assert.IsTrue(xDocumentOriginal.Root.Attributes().Where(attr => attr.IsNamespaceDeclaration).Count() > 0);
            
                                                            var xDocumentNew = XDocument.Parse(xDocumentOriginal.Root.RemoveNamespaces().ToString(), LoadOptions.None);
                                                            Assert.IsNotNull(xDocumentNew);
                                                            Assert.IsNotNull(xDocumentNew.Root);
            
                                                            Assert.IsTrue(xDocumentNew.Root.Attributes().Where(attr => attr.IsNamespaceDeclaration).Count() == 0);
                                                        }
                                                    }
                                                }
                                                

Tip: SyntaxHighlighter

sep 30, 2011 22:38:55   //   Geert Van Huychem   //   Freebies   //   0 reacties

SyntaxHighlighter is an open source Java Script client side code syntax highlighter.

SyntaxHighlighter uses separate syntax files called brushes to define its highlighting functionality.

You can download the latest version or watch demos of SyntaxHighlighter here

Demo

                                    // SyntaxHighlighter makes your code snippets beautiful without tiring your servers.
                                    // http://alexgorbatchev.com
                                    var setArray = function(elems) {
                                        this.length = 0;       
                                        push.apply(this, elems);
                                        return this;
                                    }
                                

Tip: MVC - Text input - Validate Xss

sep 30, 2011 22:38:55   //   Geert Van Huychem   //   Freebies   //   0 reacties

Pretty sure you came across this one: "A potentially dangerous Request.Form value was detected from the client", when you tried to add html tags in a blog post, for example.

To avoid this error in ASP.NET, you just had to add ValidateRequest="false" to the page directive, or in MVC add the ValidateInput(false) attribute to the controller method

Fairly easy to avoid the error, but now you're page is no longer secured for XSS attacks. One way to avoid this is filtering the user input, for example by using HtmlAgilityPack, as illustrated in the code below

Major advantage of this approach: it also removes encoded script tags. Actually, you can remove just about anything, like images if you want to.

                                    using HtmlAgilityPack;
            
                                    namespace iFrameWorx.Core.Utilities.Extensions.String
                                    {
                                        public static class ValidateXssExtension
                                        {
                                            public static string ValidateXss(this string input)
                                            {
                                                try
                                                {
                                                    var html = new HtmlDocument { OptionFixNestedTags = true, OptionAutoCloseOnEnd = true };
            
                                                    html.LoadHtml(input);
            
                                                    //  remove all scripts
                                                    var scripts = html.DocumentNode.SelectNodes("//script");
                                                    if (scripts != null)
                                                        foreach (var script in scripts)
                                                            script.Remove();
            
                                                    return html.DocumentNode.OuterHtml;
                                                }
                                                catch
                                                {
                                                    //  ignore error, return input
                                                }
            
                                                return input;
                                            }
                                        }
                                    }
                                    using iFrameWorx.Core.Utilities.Extensions.String;
            
                                    using NUnit.Framework;
            
                                    namespace iFrameWorx.Core.UnitTests.Extensions
                                    {
                                        [TestFixture]
                                        public class ValidateXssUnitTest
                                        {
                                            [Test]
                                            public void ValidateXss()
                                            {
                                                var s = "<script>alert('Hello!');</script>";
            
                                                s = s.ValidateXss();
            
                                                Assert.IsNullOrEmpty(s);
                                            }
            
                                            [Test]
                                            public void ValidateXssEncoded()
                                            {
                                                var s = "\x3cscript\x3e%20alert(\x27Hello!\x27)%20\x3c/script\x3e";
            
                                                s = s.ValidateXss();
            
                                                Assert.IsNullOrEmpty(s);
                                            }
                                        }
                                    }

Tip: Cleanup HTML

sep 30, 2011 22:38:54   //   Geert Van Huychem   //   Freebies   //   0 reacties

Just a few lines of code I put together to clean up html input, by removing carriage returns, redundant white spaces (as much as possible...), HtmlAgilityPack takes care of missing closing tags. There's a little bug here in HtmlAgilityPack, it doesn't close missing <p>-tags...

                                    using System.Text.RegularExpressions;
            
                                    using HtmlAgilityPack;
            
                                    namespace iFrameWorx.Core.Utilities.Extensions.Html
                                    {
                                        public static class CleanupHtmlExtension
                                        {
                                            public static string CleanupHtml(this string input)
                                            {
                                                try
                                                {
                                                    var html = new HtmlDocument { OptionFixNestedTags = true, OptionAutoCloseOnEnd = true };
            
                                                    html.LoadHtml(input);
            
                                                    var cleanHtml = Regex.Replace(html.DocumentNode.OuterHtml, @">\s*<", "><", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            
                                                    cleanHtml = Regex.Replace(cleanHtml, @"(<[^/>]*>)\s*", "$1", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                                            
                                                    cleanHtml = Regex.Replace(cleanHtml, "\r\n|\r|\n", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);
                            
                                                    cleanHtml = Regex.Replace(cleanHtml, @"\s+", " ", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            
                                                    return cleanHtml;
                                                }
                                                catch
                                                {
                                                    //  ignore error, return input
                                                }
            
                                                return input;
                                            }
                                        }
                                    }
                                

The one thing I can't cleanup is this one: whitespace followed by opening tag, because that would break up the text.

If I add the functionality to replace "...end of the sentence. </p>" with "...end of the sentence.</p>", that would also mean that "...middle of the sentence <i>some word<i>..." would be replaced with "...middle of the sentence<i>some word<i>...", which is obviously wrong, so it's better to leave this 'as is'.

Note the whitespace before on line 24.

                                using iFrameWorx.Core.Utilities.Extensions.Html;
            
                                using NUnit.Framework;
            
                                namespace iFrameWorx.Core.UnitTests.Extensions
                                {
                                    [TestFixture]
                                    public class CleanupHtmlUnitTest
                                    {
                                        [Test]
                                        public void CleanupHtmlExtension()
                                        {
                                            const string input = @"<b>
            
                                                <img src='http://datanews.rnews.be/images/resized/119/501/349/393/9/200_0_KEEP_RATIO_SCALE_CENTER_FFFFFF.jpg'>
            
                                                    <u><li>
                                                De Amerikaanse zangeres Lady Gaga is de eerste beroemdheid geworden van wie de berichtjes op Twitter gevolgd worden door meer dan tien miljoen internetgebruikers.
                                                        </li>
                                                ";
            
                                            var s = input.CleanupHtml();
            
                                            Assert.AreEqual(@"<b><img src='http://datanews.rnews.be/images/resized/119/501/349/393/9/200_0_KEEP_RATIO_SCALE_CENTER_FFFFFF.jpg'><u><li>De Amerikaanse zangeres Lady Gaga is de eerste beroemdheid geworden van wie de berichtjes op Twitter gevolgd worden door meer dan tien miljoen internetgebruikers. </li></u></b>", s);
                                        }
                                    }
                                }
                                

Tip: Search File Contents

sep 30, 2011 22:38:53   //   Geert Van Huychem   //   Freebies   //   0 reacties

When using the Windows Search functionality, you can search for words inside files. By default this doesn’t seem to work for .sql or .php files however.

To enable this, add the following key to the registry:

HKEY_CLASSES_ROOT\.sql\PersistentHandler\(Default) : {5e941d80-bf96-11cd-b579-08002b30bfeb}

This will associate the plain text filter (System32\Query.dll) to the specific file type, making it possible to search the content of the file. The reason not all extensions have this enabled by default, is due to performance considerations from Microsoft’s point of view. I personally haven’t noticed any visible performance problems from enabling it for SQL and PHP files.

Log off and log on again. From then on you can use the search box to search inside SQL and PHP files. A quick way to make Windows recognize the change, is by killing the explorer.exe process and starting a new one. That way you don’t have to close any programs.