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);
                                            }
                                        }
                                    }