Tuesday, 30 March 2010

Internet Explorer Profiling Tool

John Resig (Creator of jQuery) posted a link recently to an excellent client side tracing / profiling tool for Internet Explorer - DynaTrace. Check it out:

http://ejohn.org/blog/deep-tracing-of-internet-explorer/

Friday, 19 March 2010

Linux disk free space

All system admins will need to check, at some point, how much disk space is free on their systems. If the system is remote, or perhaps in a datacenter, you will need to run a shell command. the one you want is:

> du

This basic command outputs the space in terms of 1 kilobyte chunks, which isn't very readable. to get a nicely formatted output, run this:

> du -h

Nice and easy huh? Now why not script it and put it into a cron job??

Localhost not working in fiddler - a solution

I was testing my .net site in Internet Explorer today using Fiddler. The Visual Studio development server uses localhost with a random port. However, Fiddler will not captures responses from localhost.

Don't worry, though. There's a neat trick you can do to fix this localhost error. Let's use an example. If you are testing the page:

http://localhost:2622/MySite/Default.aspx

simply change it to:

http://somesite.com:2622/MySite/Default.aspx

The url http://www.somesite.com points to localhost. 

This is a really neat trick for working around the fiddler localhost restriction.

The proof:
Hooray!

Monday, 15 March 2010

Consuming ASP.NET AJAX Web Services in jQuery

Nice clear information.

This can also be used for Page Methods, if you prefer to avoid sending the SOAP header. However, you will need to Serialize the response as JSON.

Link below:

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

Saturday, 13 March 2010

IIS7 rewrite rule for requests from the local network

First, if you don't know about the IIS7 rewrite module, read my earlier post on it.

You back? Good.

So today I was trying to debug my web app. My web server's internal ip is 192.168.1.80, but whenever I typed this into my browser, it would send me to http://www.192.168.1.80

I discovered that this was because of one of the rules I setup earlier in the IIS7 rewrite module:

<rule name="Non www to www redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.yoursite\.com$" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>

Can you see why? An IP address clearly doesn't match the pattern. To solve this problem, I just added a second condition to the rule:

<rule name="Non www to www redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.yoursite\.com$" ignoreCase="true" negate="true" />
<add input="{HTTP_HOST}" pattern="^192\.168\.*" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>

So what is this doing? Well, it's simply saying: "If the url contains the full site name, or the request comes from inside my network, ignore this rule". Or rather, it's APPLYING the rule to anything that DOESN'T match the conditions (that's what the "negate" attribute means).

That's why I love the IIS rewrite module. I love the way you can stack conditions like this. It's nice and understandable, and easy enough to change to do whatever you want.

Friday, 12 March 2010

Granting permissions to another computer in Windows

Hello there!

Do you ever get confused about which user to grant permission to on a specific folder?

What if you want to simply want to grant any access requests from another computer? (perhaps the other computer is a front end server - in web hosting, it's common practice to store your static files on another server).

One other reason to do this is so that you don't have to set up asp.net impersonation to give your asp.net website access to a network resource (the most common way of doing asp.net impersonation is storing cleartext passwords in web.config, which is kinda frowned upon).

Well, all you need to do to work around all the above issues is give the other computer itself permissions, then any user on the other computer will inherit the permissions (like the network service/aspnet user, for example):

  1. Open up the permissions/properties dialog for the folder (XP/2003 dialog shown below - in Vista/2008 you would right click and go to Sharing/Permissions).
  2. Click Add.
  3. By Default, the only "objects" you can share with are Users, Groups and Security Principals.
  4. Click on the "Object Types" button and check "Computers".
  5. Under "Enter the object names to select", type the other computer's name followed by a dollar ($) sign. For example "WEB$".
  6. Click "Check Names".
  7. Once the name is verified, click OK. You will now see something like this:


Now just set up your permission - this depends on what you want the other computer's users to be able to do (in my case any user on the WEB server). At the most, you should only need to give read and write access. Higher permissions like List Folder Contents, Modify and Full Control should only be granted to the Administrators group.

By the way, if you're running IIS7 remember to set the Application Pool to use the NETWORK SERVICE user, not the default IUSR, or this won't work. I'll detail how and why in a future post.

Help! My HTTP Handler doesn't work in IIS7!

I faced this problem earlier today. Thankfully (as is usually the case) it was simple to resolve.

If you face this problem with an httphandler (usually .axd extension), make sure you have added a new "system.webServer" section to your web.config file. Yes, that's right, IIS7 doesn't look at the old httpHandlers and httpModules conig sections in system.web. You will need to copy your these sections into the new system.webServer section.

But wait, there's more! What caught me out today was the fact that IIS7 needs to see a name attribute in each "add" element. I am using a third party captcha on one of my sites - Lanap Botdetect. After puzzling over a 500.19 server error about incorrect configuration, I realised that IIS7 was expecting to see a name attribute, so I simply added name="LanapCaptcha" to the element (see below). Problem solved. I you have any specific questions related to httphandler problems, post them in the comments section below and I'll try and help you. If this post helped you, please Digg it using the Digg button at the top!

</system.webServer>

...

<handlers>

<remove name="WebServiceHandlerFactory-Integrated"/>

<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

<add name="LanapCaptcha" path="LanapCaptcha.aspx" verb="*" type="Lanap.BotDetect.CaptchaHandler, Lanap.BotDetect" />

<add name="ASBXHandler" verb="GET,HEAD,POST" path="*.asbx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

</handlers>

</system.webServer>

Monday, 8 March 2010

The power of the IIS7 url rewrite module

Having only recently got into IIS7 after buying a new web server, I am pleased to see that version 7 is somewhat following apache's lead by allowing modules to be plugged into the server. If you host anything beyond a very basic website in IIS 7, you will likely have the need to use http modules, very likely including some kind of url rewiting.

There can be many reasons to do url rewriting - Search Engine Optimization (friendly urls), Redirection to a secure site, or changing the request in some other way.

With IIS 6 you would either have to pay for a 3rd party ISAPI dll to do some of these, or spend time programming them yourself. But now the IIS 7 url rewrite module is here to come to the rescue.

All this involes is editing your web.config, using the new IIS7-only system.webServer configuration section (you can also do it through IIS Manager). Here are 2 examples.

The first rule I have created below redirects requests starting with "http" to the equivalent "https" url. You can do this for a specific area within your application too - just add another web.config within that subfolder with appropriate rules inside it's system.webServer -> rewrite section. You can also use tags if you want to keep all web.config stuff in one file. E.g.:

<location path="admin"><system.webserver><rewrite>... your rules...</rewrite></system.webserver></location>

The second rule redirects requests not containing www (e.g.: http://yoursite.com) to the url containing www. You can do the opposite quite easily too.

<system.webServer>
...
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<!-- Require SSL must be OFF in the site settings -->
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="Non www to www redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.yoursite\.com$" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>

See also the official documentation for the IIS7 url rewriting module.