We have an existing application that was created on ASP.NET MVC 4 and the web API. The administrative parts of the site use Simple Membership. I am interested in upgrading the application to MVC 5 / Web API 2 to take advantage of some of the new features added. But it looks like they may be incompatible.
In particular, after installing RC packages from NuGet to one of the projects in my solution and updating the web.config information , the application starts dying during startup on the line that calls WebSecurity.InitializeDatabaseConnection() , with this exception:
[MethodAccessException: Attempt by security transparent method 'WebMatrix.WebData.PreApplicationStartCode.OnConnectionOpened(System.Object, WebMatrix.Data.ConnectionEventArgs)' to access security critical method 'System.Web.WebPages.HttpContextExtensions.RegisterForDispose(System.Web.HttpContextBase, System.IDisposable)' failed.] WebMatrix.WebData.PreApplicationStartCode.OnConnectionOpened(Object sender, ConnectionEventArgs e) +70 WebMatrix.Data.Database.OnConnectionOpened() +70 WebMatrix.Data.Database.EnsureConnectionOpen() +51 WebMatrix.Data.Database.QueryValue(String commandText, Object[] args) +63 WebMatrix.WebData.DatabaseWrapper.QueryValue(String commandText, Object[] parameters) +13 WebMatrix.WebData.SimpleMembershipProvider.GetUserId(IDatabase db, String userTableName, String userNameColumn, String userIdColumn, String userName) +206 WebMatrix.WebData.SimpleMembershipProvider.ValidateUserTable() +87
Other projects in one solution using Simple Membership that I have not updated continue to work fine.
For more information, Google displays many hits for this exception, of course, but nothing special for WebMatrix.
FWIW: I know that Microsoft has implemented (another) solution for membership and identification , but if there is no way to use it from existing Simple Membership tables or a seamless migration path for all our existing user data, which is not really an option for us.
UPDATE (October 11th)
I just tried it again with a fresh check of the current trunk of our application. I use Visual Studio 2012, but otherwise I followed instructions from MS to update an existing project. After upgrading to MVC 5 / Web API 2 / EF 6, the application launched the "simple" mode.
In web.config there were no explicit trust requirements for deletion. I added the code from this question to Global.asax.cs , and it reports that the application works with full trust (in IIS Express, only F5-ed from VS).
Re-adding the same call to InitializeDatabaseConnection() , it starts to die with the same exception.
DECISION (October 28)
Having tried the solution in updating @Kevin from Friday, I found that it works. It was very strange to me that adding this obviously unrelated package would solve these security problems, and even more strange after I removed the package from my solution and continued to work.
After carefully examining what was happening, I realized that the reason for this fix is โโquite simple: the Microsoft.AspNet.WebHelpers package has two dependencies that were added to my solution: Microsoft.AspNet.WebPages.Data and Microsoft.AspNet.WebPages.WebData . Microsoft has moved WebMatrix classes to new packages.
So, the added package of helpers fixed the problem, not because of anything, but since it caused updated versions of broken assemblies to add to my solution. So the solution to the initial incompatibility is to install these new packages while updating everything else from NuGet:
Install-Package Microsoft.AspNet.WebPages.WebData
UPDATE (May 13, 2015)
It has been suggested that you might also need to manually install a second new package:
Install-Package Microsoft.AspNet.WebPages.Data
This is not necessary because this package is an explicit dependency on the former, and NuGet must be smart enough to install both. But if you get an error while creating it or donโt see NuGet add dependency, this may help you.