Compiling a .Net DLL for a medium trust environment

18. September 2008

Recently I wrote a little utility for a client using the excellent Html Agility Pack to read and navigate through a HTML page, selecting the data that was needed and parsing it - basically a screen scrape. I downloaded the source, compiled it, added a reference to the dll in my project and tapped away for a few minutes and et voila, within a few minutes a working screen scrape. A fantastic library.

On uploading the project to my GoDaddy web hosting however, I encountered a problem. You see, my hosting is a shared hosting environment, and like most such webhosting environments is set to a Medium Trust level for .Net applications. As MS dryly puts it:

Applications that receive less than full trust by the runtime code access security system are not allowed to call shared managed libraries unless the library writer specifically allows them to through the use of the AllowPartiallyTrustedCallersAttribute Class. Therefore, application writers must be aware that some libraries will not be available to them from a partially trusted context.

The solution, although slightly confusing from the MS documentation, is actually very simple. I opened the HtmlAgilityPack source code, and edited the AssemblyInfo.cs file. Firstly, add a reference to the AllowPartiallyTrustedCallersAttribute:

[assembly: AllowPartiallyTrustedCallersAttribute()]

Since AllowPartiallyTrustedCallersAttribute is part of the System.Security namespace, we must add a reference at the top of the page:

using System.Security;

I then rebuilt the project, rebuilt the web project and it now works like a charm.

ASP.NET , , ,

Restoring the ASP.NET tab in IIS

4. April 2008

If you've logged onto the properties for your IIS install and found that the ASP.NET tab has mysteriously disappered, you can try a couple of things.

Firstly, try re-registering ASP.NET with IIS using the ASPNET_REGIIS.exe located in the .NET installation folder:

c:\WINDOWS\MICROSOFT.NET\framework\\aspnet_regiis -i

 

Chances are though, that it won't work, and that you can try and number of command using aspnet_regiis.exe or even uninstalling and reinstalling .NET and you won't actually fix the problem. (Note: on x64 systems this could be a different problem, Google again with x64!)

This was solved by switching a flag (Enable32BitAppOnWin64) to false in the IIS metabase:

cd c:\Inetpub\AdminScripts\

cscript.exe adsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 FALSE

You should then be able to see the ASP.NET tab restored in the site properties.

IIS, ASP.NET , ,

Installing Visual Studio 2003 SP1 on Vista

28. June 2007
I’ve finally cracked the installation problems with VS 2003 SP1 on Vista. The problem seems to be that the SP must be installed under the same credentials that Visual Studio was installed. I.e. I installed Visual Studio under the domain administrator credentials, but I run my day-to-day under a standard user credential. When installing the SP1 I was elevating my standard user credential rather than running as the domain admin. I don’t know what difference that makes to anything, but it’s worked!

 

 Where was that purchase requisition for 2005?

ASP.NET, Windows Vista ,

Visual Studio Project Location Not Trusted

8. June 2007

Just a quick post today about trust levels for .NET assemblies that are hosted remotely. My current set up at work means that I am maintaining one version of our web site while working on developing a new one - not uncommon. I have 2 virtual servers running Server 2003, IIS and SQL Server, each an identical copy of our production server. Each solution and it’s projects are stored on each virtual server, with the project folders shared and mapped as drives on my laptop. (Before I get messages saying “why don’t you use source control, you can create branches etc, etc” - I am aware of that and the decision not to use source control is based on other factors that I’m not going to get into.)

 Anyhow, on to the problem and, the solution.

When opening a Visual Studio solution from a mapped or network location, you get a “project location not trusted” error. By default, your .NET configuration grants FULL trust to your local machine, so you won’t get that message working on a local project. Also by default, the Local Intranet is given a step below full trust - and here is the problem. You will need to be a local administrator on your computer to make these changes.

Open a command prompt, navigate to:

  1. cd %systemroot%\Microsoft.NET\Framework\\

Run the following command:

  1. CasPol.exe -pp off -m -ag 1.2 -url file://r:\folder\* FullTrust

or, for an unmapped folder

  1. CasPol.exe -pp off -m -ag 1.2 -url file://computername/folder/* FullTrust

This will set the share to full trust and allow you to work with the remote folder/mapped drive as if it’s on your local machine.

For more details about using CasPol.exe and a break down of the above commands, check out the .NET Security Blog

ASP.NET, Visual Studio, Windows Server 2003