ASP MVC 5 applications stopped working with "System.Security.SecurityException' in mscorlib.dll"

troubleshooting asp.net web applications
troubleshoot web application and website issues
production troubleshooting .net applications
iis issues and solutions
server error in '/' application.
asp net mvc slow page load
asp.net error logging
iis troubleshooting

Partly solved the moment I hit save: The Exception settings of the failing solution were set to break on all for Common Language Runtime Exceptions". Resetting those to default allowed the project to start.

This appears to be a solution level issue.

I have a solution with half a dozen projects in including a web project created as an MVC project.

I've not made any changes to it. it's been in the solution for a couple of months.

Today it no longer run. Initially it threw an exception saying it couldn't access Temporary ASP.NET files in the ridiculous default location of C:\Windows... So I fixed that by adding tempDirectory="..."

The next exception that occurs is:

System.Security.SecurityException occurred
Message: Exception thrown: 'System.Security.SecurityException' in mscorlib.dll
Additional information: Requested registry access is not allowed.

The stack trace at this point is:

mscorlib.dll!System.ThrowHelper.ThrowSecurityException(System.ExceptionResource resource)   Unknown
mscorlib.dll!Microsoft.Win32.RegistryKey.OpenSubKey(string name, bool writable) Unknown
mscorlib.dll!Microsoft.Win32.RegistryKey.OpenSubKey(string name)    Unknown
System.Web.dll!System.Web.Util.Misc.OpenAspNetRegKey(string subKey) Unknown
System.Web.dll!System.Web.Util.Misc.GetAspNetRegValue(string subKey, string valueName, object defaultValue) Unknown
System.Web.dll!System.Web.Compilation.CompilationMutex.CompilationMutex(string name, string comment)    Unknown
System.Web.dll!System.Web.Compilation.CompilationLock.CompilationLock() Unknown
[Native to Managed Transition]  
[Managed to Native Transition]  
System.Web.dll!System.Web.Compilation.CompilationLock.GetLock(ref bool gotLock) Unknown
System.Web.dll!System.Web.Compilation.BuildManager.CheckTopLevelFilesUpToDate(long cachedHash)  Unknown
System.Web.dll!System.Web.Compilation.BuildManager.ExecutePreAppStart() Unknown
System.Web.dll!System.Web.Hosting.HostingEnvironment.Initialize(System.Web.Hosting.ApplicationManager appManager, System.Web.Hosting.IApplicationHost appHost, System.Web.Configuration.IConfigMapPathFactory configMapPathFactory, System.Web.Hosting.HostingEnvironmentParameters hostingParameters, System.Security.Policy.PolicyLevel policyLevel, System.Exception appDomainCreationException) Unknown
System.Web.dll!System.Web.Hosting.HostingEnvironment.Initialize(System.Web.Hosting.ApplicationManager appManager, System.Web.Hosting.IApplicationHost appHost, System.Web.Configuration.IConfigMapPathFactory configMapPathFactory, System.Web.Hosting.HostingEnvironmentParameters hostingParameters, System.Security.Policy.PolicyLevel policyLevel)  Unknown
[AppDomain (DefaultDomain, #1) -> AppDomain (/LM/W3SVC/2/ROOT-1-130855931402033459, #2)]    
System.Web.dll!System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironment(string appId, System.Web.Hosting.IApplicationHost appHost, System.Web.Hosting.HostingEnvironmentParameters hostingParameters)    Unknown
System.Web.dll!System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironmentAndReportErrors(string appId, System.Web.Hosting.IApplicationHost appHost, System.Web.Hosting.HostingEnvironmentParameters hostingParameters) Unknown
System.Web.dll!System.Web.Hosting.ApplicationManager.GetAppDomainWithHostingEnvironment(string appId, System.Web.Hosting.IApplicationHost appHost, System.Web.Hosting.HostingEnvironmentParameters hostingParameters)   Unknown
System.Web.dll!System.Web.Hosting.ApplicationManager.CreateObjectInternal(string appId, System.Type type, System.Web.Hosting.IApplicationHost appHost, bool failIfExists, System.Web.Hosting.HostingEnvironmentParameters hostingParameters)    Unknown
System.Web.dll!System.Web.Hosting.ProcessHost.StartApplication(string appId, string appPath, out object runtimeInterface)   Unknown

If I create a new solution with a single web MVC web project in and run it, it starts correctly, showing the standard template.

If I add a new project to the existing solution and run it, It does not run! It throws the same access exceptions.

IF I add the existing failing project to the new working solution, it works!!

I've tried deleting all temp asp.net files, all temp files generally, stopping stuff, rebooting. Nothing works.

It's as if the solution has started with fewer rights for some reason. I'm not running either instance as administrator, and using the same shortcut icon to start VS.

This is with Visual Studio 2015. Framework is set to 4.51

Any clues at all?

IF I add the existing failing project to the new working solution, it works!!

Then just create a new Solution.sln file. Your projects can stay intact, just delete / replace the old sln file with a new one.

How to Troubleshoot ASP.NET Web Application Problems, ASP.NET troubleshooting topics: Is Your Web Application Returning Lots of Retrace has a great visual for tracking your top 5 web requests. In the New ASP.NET Web Application dialog, choose MVC and then choose OK. Visual Studio used a default template for the ASP.NET MVC project you just created, so you have a working application right now without doing anything! This is a simple "Hello World!" project, and it's a good place to start your application. Press F5 to start debugging.

I get rid of this problem if I run Visual Studio 2015 as Administrator (right click Visual Studio icon -> Run as Administrator).

Troubleshooting Common ASP.NET MVC Problems, Make sure you change the .NET version with the running version for the application. <add name=".NET v4.5" startMode=  In this guide, we reviewed how to troubleshoot ASP.NET web application problems. Including problems that may be affecting your entire application as well as specific web requests. Retrace is designed for troubleshooting application problems. It provides a holistic approach to application monitoring.

Using Registry Editor, find this key

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET

right click, "Permissions...", "Advanced"

and add IUSR and "Allow Read" and apply it to "This keys and subkeys"

Troubleshooting common permissions and security-related issues in , The focus of this column will not be to explain ASP.NET security, but to outline a few troubleshooting techniques and tips that will NET applications on the server fail with the same error? In IIS 5.x you would change the Creating ASP.NET MVC 5 Web Application. In this section you will create the web application in the Visual Studio 2013 using the following procedure. Step 1: Open Visual Studio 2013 and click on "New Project". Step 2: Select the ASP.NET Web Application and enter the application name. Step 3: Select the MVC Project Template to develop the application.

Professional C# 7 and .NET Core 2.0, ASP.NET Web Forms is no longer part of ASP.NET Core. However, having NET MVC, you must do more work to move the application to the new technology. How to deploy ASP.NET MVC 5.0 projects to a server. Web Deployment Overview for Visual Studio and ASP.NET; Migrating ASP.NET MVC Applications to Windows Containers; Continuous Integration and Continuous Delivery (Building Real-World Cloud Apps with Azure) ASP.NET Web Deployment using Visual Studio; ASP.NET 4 - Enterprise Deployment Introduction

Pro ASP.NET MVC 5, the iis application server. one problem is that the classes that the asp.net platform uses to provide information about requests are not well-suited to unit testing,  Yes, I know, .NET 4.5 isn’t available for Windows 2003 Server. Patches are available from Microsoft to update browser detection for older framework versions. (If you tried installing before mid-December 2013, uninstall it and try again.) If you are still reading, then you may need to update the actual application. Open the App_Browsers folder.

Troubleshoot ASP.NET Core on Azure App Service and IIS , Provides troubleshooting advice for apps deployed to Azure App Service. NET Core project defaults to IIS Express hosting during debugging. The PowerShell script configures WER to collect up to five dumps per app. For ASP.NET MVC sample projects, the obvious place to look is Microsoft. As the creators of ASP.NET MVC, they understand new developers need examples to understand new concepts. Two projects that stand out in my mind are: The MVC Music Store. This tutorial application is an ASP.NET MVC Sample Project, but it's a little dated.

Comments
  • Have you tried just creating a new solution.sln file and re-adding all of the projects to it? If you can just replace the .sln file, that's an easy fix. Also make sure you deleted the hidden .suo file.
  • I really didn't want to recreate the solution file. The solution contains quite a few projects and a number of solution folders holding various other assets. It would probably only take 15 minutes or so to do, but as I didn't know what caused the original to break, I didn't want to be creating a new solution file repeatedly.
  • If the project is under source control, you could create the new solution file, then compare the old one to the new one to find out what was causing problems. I suspect maybe it is the solution level build configurations?
  • Yep it turned out to be the exception settings, it was breaking on all exceptions for all framework types. I think that setting lives in the suo file as it's user specific.