Picture this: You are demonstrating a sample. You have done the same thing earlier. It always worked. And then it failed with a cryptic exception you never saw before.
Unhandled Exception: System.ServiceModel.AddressAccessDeniedException: HTTP could not register URL http://+:2222/. Your process does not have access rights to this namespace
This is exactly what hit me while demonstrating a WCF example for the first time on Windows Vista. Of course it was only later I realized it is because of windows Vista. In fact this is what anybody running vista is bound to come across. So I felt duty bound to document it.
Back to the story, it wasn’t really obvious since this was not the first time I was running a WCF application on Vista. I was just demonstrating it for the first time. So WCF and Vista works. There must be a third factor and that is Http. I was using an Http binding for this demonstration.
The main reason for this behaviour is the new security feature of Windows Vista. In the new Operating System all Http Ports and by default reserved to be used by the administrator and by default you no longer use administrator account to execute any application. In Windows Vista, even if you are an administrator the application continues to run in the normal privilege. So there are two different ways we can address this issue:
Run As Administrator
While starting the WCF service, make sure you run the application under elevated permission. You can do so either by –
- If you need to run it just once (may be for demonstration): Right-click the application icon and select Run as Administrator.
To Run Several times (Mostly a implemented system):
- Create Applications Shortcut.
- Right Click the shortcut icon -> select properties ->Advanced
- Select the check box "Run as Administrator". Press Ok->Ok.
- Now run the application using the shortcut.
- Start a cmd window with Run as Administrator privilege and start the application.
This approach however, will fail if you want to execute the code from within Visual Studio by pressing Ctrl+F5 or simply F5. Need not add not debug friendly. But above all we did nothing to address Http or security. So let’s try the second approach.
Delegate Port to the User Account (under windows vista)
Sounds more geek. Isn’t It. But that is exactly what we need to do. As already discussed, it is Administrator that owns all the Http Port. In the first solution we called administrator to come and execute our application. In this solution, the administrator can delegate a particular Http Port and a Url to another user (under whose account you intend to run the service). So let us see how we can achieve this:
- Start Administrator Command Prompt:
Yes this is a one time and compulsory requirement. We can create a shortcut to command prompt and run it with administrative privilege. You may like to see the step 1.b above to elevate the permission of command window (cmd).
Delegate a particular port and URL to a different non-administrative user: we need to execute the netsh command with these options
C:\> netsh http add urlacl url=<actual_url_to_be_delegated> user=<domain\user>
For my exception above I used:
C:\> netsh http add urlacl url=http://+:2222/ user=Aum\Vivek
Once the activation is done your application works without any problem.
Delegate Port to the User Account (under windows 2003)
The netsh tool discussed in the previous section is new to windows vista and is likely to be available in the future OS releases. For windows server 2003, however, it doesn’t apply. we use the native 2003 tool httpcfg instead. While most of the step discussed above holds good here also, the syntax of httpcfg is a bit different and a lot for demanding.
c:\>httpcfg set urlacl /u http://URL:Port/ /aACL
c:\>httpcfg.exe set urlacl /u http://myhost:2222/ /a "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"