NHibernate, Castle IOC and MVC.NET 2 running in medium trust on shared hosting

Saturday, November 28th, 2009

This has caused me a lot of pain today so I thought that I would share my findings!

If you intend to publish your site to shared hosting and they are restricting you to a Medium trust security policy (which is expected really on shared hosting) you need to have assemblies that work properly!

After finding the issue the first thing I did was to get a dev environment that replicated the problem. The easiest way to do this is to set the trust level in the web.config. You can do this by adding this within the system.web  element:

<system.web>
<trust level=”Medium”/>

…other tastey elements…..

</system.web>

In the future I will set this before I start programming anything - probably good practice when you know you will be deploying to a medium trust environment but I will put that down to a lesson learnt!

After that you need to build Castle from source with a few additional parameters to make it run in medium trust. To do this you need to check it out from SVN. I had a few problems here too with SVN externals. I ended up getting the latest version of tortoise and checking it out on a Windows 7 VM I had running.
Check it out from here: http://svn.castleproject.org:8080/svn/castle/trunk/

Once fully checked out, open a cmd promt and navigate to the the root of the checked out directory. run this command:
build.cmd -D:assembly.allow-partially-trusted-callers=true release quick build

Watch the window for any errors and please read the error messages if you get any. I got one in relation to not having the .NET 2.0 SDK installed, so guess what - I installed it and tried again and it worked!

OK great now we have some assemblies that will run under medium trust. Copy these over the old versions and make sure you perform a clean build. Thats Castle sorted right? WRONG! It still won’t work.

I then had an issue with Castle.Service.Transations. Read this for more information (http://stackoverflow.com/questions/1038914/using-castle-windsor-and-the-nhibernate-facility-on-shared-hosting). OK no probs got that sorted by registering it in my container:

container.Register(Component
.For(typeof (IActivityManager))
.ImplementedBy(typeof (TLSActivityManager)));

OK we are still not there - a couple more things to do in the web.config. You need to add the requirePermission=”false” attribute the the castel section:

<section name=”castle” requirePermission=”false” type=”Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor”/>

I also disabled the NHibernate reflection optimiser:

<item key=”reflection-optimizer”>false</item>

OK almost there - the app will now run. Next was the problem of URL’s and the fact I was not allowed a wild card mapping - fine - no probs, lets update the routing. I changed my routing to the following:

routes.MapRoute(
"Default",
"{controller}.aspx/{action}/{id}",
new {controller = "Home", action = "Index", id = ""},
new[] {”ANAMESPACE.Core.Controllers”});

Yay! That works! There is only one more problem. If I go to / then it can’t find a route regardless of the default file settings. So a bit of a workaround was to add another route after the usual default:

routes.MapRoute(
"",
new { controller = "Home", action = "Index", id = "" },
new[] { “ANAMESPACE.Core.Controllers” });

This feels very wrong and I don’t like it! There must be a better way.

Anyway - screw you shared hosting and medium trust!

aspnet_regiis - the guide on usage

Monday, December 15th, 2008

using ‘aspnet_regiis -i’ is not always required - sometimes you might just need to be a little more gentle:

http://msdn.microsoft.com/en-us/library/k6h9cz8h(VS.80).aspx

Scheduling IIS to restart each night automatically

Monday, July 28th, 2008

Going to try using a .bat file and the net commands such as

NET STOP W3SVC
NET START W3SVC

these were useful:

http://www.ss64.com/nt/net_service.html

http://dontknow-syndrome.blogspot.com/2008/01/dos-dir-cmd.html

IIS on XP increasing the number of connections - HTTP 403.9 error

Thursday, June 19th, 2008

This can be very annoying sometimes. I know it is not best to be running IIS on XP but sometimes you just have to. When running a local host you may want another member of your team to view your site or you may be running some regretional testing such as selenium test runner and you need to be able to hit the site more than just the once. Instead you just get a 403.9 error.

To alleviate this problem (not fully) you can do the following:

  • remove HTTP Keep-Alives Enabled
  • reduce the connection timeout
  • and then run this: adsutil set w3svc/MaxConnections 40 Found: c:/Inetpub\AdminScripts

http://weblogs.asp.net/cazzu/archive/2003/10/10/31476.aspx

Windows server 2003 resource kit

Monday, April 28th, 2008

http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en

http://www.windowsnetworking.com/articles_tutorials/Windows-Server-2003-Resource-Kit.html

Removing .net version from http headers

Thursday, March 6th, 2008

To get rid of the X-AspNet-Version HTTP header from the response, just copy this line into the web.config’s <system.web> section:

<httpRuntime enableVersionHeader=”false” />

http://www.alphasierrapapa.com/IisDev/Articles/XAspFilter/

Normal Password protection for IIS 6

Thursday, August 30th, 2007

http://www.hostmysite.com/support/dedicated/IIS/passwordprotect/

 

The following article explains how to create a password protected section of your website. The protected section will require users to login before they can view contents of the folders. This is a three step process; the first step creates a Windows user account with limited access to the machine, the second step removes Anonymous Access to the folder, and the third step assigns the user to the folder.
To password protect a folder, please follow these steps:

  1. Login to your server through Terminal Services or Remote Desktop Connection.
  2. Create the Windows User by following the steps in How do I create a new user on my Windows 2000 & 2003 Server?
  3. Click Start, select Programs, and then click Administrative Tools.
    • For IIS 5.0 click Internet Services Manager.
    • For IIS 6.0 click Internet Information Services.
  4. In the left column you will see the Server Name.
    • In IIS 5.0, expand the Server Name to find the domain name.
    • In IIS 6.0, expand the Server Name and then Web Sites to find the domain name
    • .

  5. Right-click on the domain name and select Properties.
  6. On the Directory Security Tab under Authentication and Access Control click Edit.
  7. Uncheck Enable Anonymous Access.
  8. Choose the level of Authenticated Access:
    • Integrated Windows Authentication - encrypts the password sent to the server (we recommend this method).
    • Digest Authentication - this level works only if Active Directory is configured.
    • Basic Authentication - sends the password across the network in clear text (we do not recommend this method).
    • .Net Passport Authentication - a web authentication service.
  9. Click Ok and then click Ok a second time.
  10. Navigate to the folder on your server containing the contents of your website.
  11. Right click the folder and select Properties.
  12. On the Security tab click Add.
  13. Enter the name of the user you created and click Ok.
  14. If you wish to allow other users to login, repeat step 14 with the additional user names.

Your website is now password protected. If you prefer to password protect only a folder, rather than your entire website, you can repeat the exact steps above on the individual folder, rather than your entire website.

IIS Password Restriction

Tuesday, August 21st, 2007

http://www.troxo.com/downloads/downloadcenter/?action=step2&rfd=1&download=1

IIS 6 Hardening

Wednesday, August 8th, 2007

http://www.shebeen.com/win2003/

http://www.microsoft.com/technet/security/tools/locktool.mspx

Scheduling tasks on Windows Server 2003 using c#

Sunday, July 22nd, 2007

http://www.codeproject.com/csharp/TSNewLib.asp