Posted by: lluppes | December 8, 2013

MVC5 Authentication Roles Failure

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.

Advertisements

Responses

  1. Same problem here. Your solution is working O_o. But also doesn’t make any sense to me, too.

  2. Thanks! I think I read most of the same dead-ends as you did. Very weird solution, but worked for me too.

  3. A million thanks! Spent so much time before hitting this, and glad you pointed out all the same red herrings I found too!

  4. thanks helped al ot.
    only thing I can think that would cause it is the machine config

  5. thanks man saved me a load of time. Just plain weird.

  6. Its actually a nice and helpful piece of information. Im glad that you shared this useful information with us. Please keep us up to date like this. Thanks for sharing. eceeckeaecdd

  7. WIsh I had found this blog 6-8 hours ago. Thank you. Worked for me.

  8. cost me 2 days then I found this solution now works like a dream. Thank you so much. At least someone smart enough to figure this out.

  9. Hi Lyle, you are a true hero! Thank you so much. You saved me tons of searches and researches! I

  10. This MVC5 stater template has it all setup with database first identity, sql scripts, and other nice additions. It saved me a lot of time. http://inovacode.com/MvcStarterKit.html

  11. I agree, same issue. set enableSimpleMembership did not work, removing webmatrix neither. remove name=”RoleManager worked. Thanks for your post.

  12. Same issue. setting enableSimpleMembership to False did not work, removing webmatrix neither. remove name=”RoleManager helped me out. Thx for your post

  13. THANK YOU SO MUUUUUUCH!!

  14. Thank you very much.

  15. Thanks for sharing, you saved me a lot of time!

  16. Thank you so much! I spent half a day on this and tried all kinds of suggestions and yours worked right away.

  17. it wasted 10 hours of mine. the got solution from this resource

  18. It worked! Thanks.

  19. Thank you so much, I just ran into this problem and I was going insane trying to fix it.

  20. Thank you, THANK YOU! This is the magic bullet. Fought with this on and off for 3 months.

  21. Thank you so much !! I Just wasted nearly 12 hours for a single line. Thank you for saving me 🙂

  22. Oh fantastic! I have ages trying to work out why when I published to Azure this error occurred. Was working fine locally but not on Azure… If there was an award for helping others Lyle, you just won it!!

  23. Thanks Lyle, wasted too much time on this one – had no problems locally but when I deployed on Azure… ooch!! Happy Christmas!

  24. You are amazing. This simple solution saved me so much pain. Just too bad i checked a about 20 solution before this one.

  25. it didnt help me …i got
    HTTP Error 500.19 – Internal Server Error

  26. Thank a lot dear, we also got stuck this type of issue and spend 2 days and not find the solution,but thanks for your blog which helps us to sort out the issue.

  27. Thanks a ton! I had to go through a lot of struggle since last couple of days without any clue as to how to fix this issue. You saved me from a huge amount of frustration.

  28. Thank you! This helped a ton.

  29. 2 hours of Googling later and your 3 line fix worked perfectly! Thanks

  30. Thank you so much!! I had exact same problem and it took me few days to reach your solution!! Thanks again.


What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: