I’ve been doing a lot of work lately with ASP.NET MVC5, and with most of my development being in a corporate environment, we almost always need authentication in our projects. MVC4 introduced a new authentication model and MVC5 made major changes to the authentication model once again.
In a recent project, I was vexed with a very strange error that took me hours to resolve. On my development machine, I have SQL Server installed, but do NOT have SQL Express installed or running. Some of my other MVC5 projects were working just fine with Authentication and Roles. In this particular project, the authentication was working just fine, and was creating users in the database as they authenticated. However, as soon as you tried to check a role using either the explicit Roles.IsUserInRole method or through a standard MVC decoration like [Authorize(Roles = Constants.Role.AdminRole)], the page would sit and spin several minutes, then time out with this error.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
SQLExpress database file auto-creation error: [stack trace – blah, blah, blah]
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.33440
Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Apparently the Role Manager was attempting to create a SQL Express database in order to validate the roles, and it was unable to do so. During my searching and experimenting, I found many red herring suggestions which did not help at all. Since this project was using MVC5, the SimpleIdentity provider was NOT being used, as MVC5 uses the new AspNet.Identity libraries. So most of the suggestions on this topic offered up things about SimpleIdentity, WebMatrix, adding a <RoleManager> or “enableSimpleMembership” tag in the web.config, update InitializeSimpleMembershipAttribute, etc. – and none of these worked.
Oddly enough, what did work did not seem at all intuitive to me: removing the RoleManager in the web.config modules section.
<system.webServer> <modules> <remove name="RoleManager" /> </modules> </system.webServer>
This didn’t seem at all obvious to me since I had not actually added it, and I was not running in the context of a server which had a root web.config file which added it. I still don’t know how or why it was trying to load the older RoleManager. In any case, adding this line allowed the MVC5 Role Manager to kick in and then everything worked fabulously. As I’ve said – I’ve had other MVC5 projects which used Authentication and Roles and had no problems, but there was something different about this project which caused this failure.
I hope this helps someone else as this strange error cost me hours of down time.