Search
1
vote
closed
HtmlHelper.AntiForgeryToken causes TypeInitializationException when site is hosted in a virtual directory that contains spaces

Description

Okay, so it might not be best-practise to have spaces in directories - notwithstanding that, the irony surrounding this bug is that it's in the internal type System.Web.Mvc.AntiForgeryDataSerializer+FormatterGenerator+TokenPersister, which is prefixed with a comment saying how difficult it is to unit test (and I don't disagree with that!).

I've written a blog post on this here (http://www.lordzoltan.org/blog/post/MVC-Bug-The-virtual-path-path-maps-to-another-application-which-is-not-allowed.aspx) which has a lot more detail. But to summarise from there, this is how you can reproduce the problem:

- Create a new Asp.Net MVC2 Web Application (from the template) called ‘Asp Net Bug 2008’
- Don’t bother with the unit tests project
- Open the project web properties, and set the project to use the Local IIS Web Server, the location should automatically be set to http://localhost/Asp Net Bug 2008/ (you might need to add the trailing slash here) – the spaces here are important so leave them in.
- Create the Virtual Directory
- Don’t forget to save the project file afterwards (like I just did as I created this walkthrough!)
- Open the view Views/Account/Register.aspx
- Just after the line <% using (Html.BeginForm()) { %> add: <%= Html.AntiForgeryToken() %>
- Compile and run
- Navigate to the [Log On] link at the top of the page
- Hit the ‘Register’ link

Hey presto - you get an exception.

The problem is with how TokenPersistor fakes the request to initialise the IStateFormatter in CreateFormatterGenerator.

If you simply create an Asp.Net WebForm and simply try to access the Browser property (which is where this error originates a bit further down the stacktrace), it works fine.

Could it be that the url encoding on the current Request.Url needs to be undone when being fed into the dummy page so that the virtual path check (to make sure the Url is within the current application) succeeds?

File Attachments

No files are attached

Closed Dec 11 2010 at 12:35 AM by marcind

This problem should no longer occur in MVC 3 as the troublesome code path going through AntiForgeryDataSerializer+FormatterGenerator+TokenPersister has been removed.


Comments

LordZoltan wrote Aug 12 2010 at 8:43 PM
Thank-you for your response. Sounds promising, and to be honest as I looked over the code it had all the hallmarks of a solution that would be under review!

Hope it is fixed!

eilonlipton wrote Aug 12 2010 at 4:43 PM
I think this issue might already be fixed in ASP.NET MVC 3 Preview 1. We changed the code path to avoid the neat little trick of using a fake page to help render the token. I'll make sure we verify this is fixed.

Updating...
© 2006-2012 Microsoft | Get Help | Privacy Statement | Terms of Use | Code of Conduct | Advertise With Us | Version 2012.2.7.18398