13

I'm having issues using the new identity system in MVC 5, my goal is to make use of the User.IsinRole("RoleName") on Views. For example:

@if(User.IsInRole("Administrator"))
 {
   <li>@Html.ActionLink("Admin", "Index", "Admin")</li>
 }

This is placed in the main layout page which is hit when the application launches. On doing this, i'm getting the following error:

"An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code

Additional information: Unable to connect to SQL Server database."

I have searched high and low for a solution to this, the common solution is to either include "[InitializeSimpleMembership]" at the top of the controller or initialise the database connection manually in application start. (With WebSecurity.InitializeDatabaseConnection). Both of these methods do not seem to be recognised by MVC 5.

I have also tried working around this by creating a bunch of messy code any time i return a view to populate the ViewBag with an IsAdmin boolean by using the Aspnet.Identity.UserManager to determine roles. Whilst this works it's not the way i feel i should be doing things.

It might be worth noting but i don't experience these issues accessing User.IsInRole on the backend, this definitely seems to be an initialization problem.

Stunt
  • 1,374
  • 2
  • 10
  • 18
  • "Unable to connect to SQL Server database" is the issue with ConnectionString. Make sure you have correct connection string and you are able to connect to Db using it. – jd4u Nov 10 '13 at 07:08
  • User.IsInRole works absolutely fine on the controllers, this is only occurring on the views. For me, that rules out connection string. – Stunt Nov 10 '13 at 10:14
  • I am not able to reproduce your problem. I create an MVC app from the VS2013 templates, select Individual Accrounts and add the code you provided to my _Layout.cs file. Works fine. There is no need for any special initialization. Are you sure that it is the User.IsInRole call that cause the exception? Have you checked the stacktrace to find what exactly where it throws that exception? – Olav Nybø Nov 10 '13 at 14:19
  • Olav, i've had a look at the stack trace and it looks like it's trying to create a local DB : "at System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString)" My difference from you at this point is i've hooked my MVC up to the local SQL2013 DB. Is this where the problem lies? – Stunt Nov 11 '13 at 17:11
  • Sorry that should read SQL2012 of course, typo! – Stunt Nov 12 '13 at 08:37
  • I have the same problem - very stupid, I am not using EF or anything- I just want IsInRole to behave normally, not try and create/use a database to determine it, I have already wired up the users context/roles, etc - very stupid problem to have – schmoopy Nov 17 '13 at 18:10
  • Have you managed to get anywhere with this yet, schmoopy? – Stunt Nov 20 '13 at 21:57

3 Answers3

23

I was having the same problem, however Stunt's answer wasn't working for me. Tried the answer to this question and that solved the issue.

For the lazy you can try adding this to your web.config file:

 <system.webServer>
    <modules>
      <remove name="RoleManager" />
    </modules>
  </system.webServer>
Community
  • 1
  • 1
grimurd
  • 2,750
  • 1
  • 24
  • 39
  • This solved my issue. Nothing was working for me. This should be marked as the answer. – Geethanga Jan 23 '14 at 14:32
  • 4
    I had to actually remove this from my config for things to work! – Chris Mar 31 '14 at 12:29
  • This worked for me as well, not sure why though, I've made previous MVC project with various versions, and never used that XML snippet before. Thanks nonetheless – nityan Nov 16 '14 at 11:08
  • I got the same problem when I opened my old project in Visual Studio 2015 Community. This mythically solved the problem. Thanks. – Vaclav Elias Jul 31 '15 at 20:58
  • Was updating a website from Asp.Net MVC 4 to Asp.Net MVC 5. Had this issue. Updating the web.config file worked for me. – user2347528 Jan 29 '16 at 22:24
  • 1
    My poor dog thanks you. He cannot understand why i was yelling at the computer so much. Can you explain why this works when `` node is removed? Some kind of 'clever' dependency injection? – Simon_Weaver Dec 14 '17 at 07:49
  • Make sure to add the `remove` node under `modules` and not `httpModules` – Simon_Weaver Dec 14 '17 at 08:10
  • Thank you! This also solved the problem for me that suddenly appeared today after working perfectly for the last 12 months. I have also used the same code for about the last 10 years, never with a problem. Any ideas why this is? Coincidence may be that i added a reference to system.web.helper in a separate project in the solution. – Toad Jul 29 '19 at 12:22
3

I managed to get around the problem by removing the following line from my Web Config:

<roleManager enabled="true" />

This was found when looking after comparing line for line on the following example code:

https://github.com/rustd/AspnetIdentitySample/tree/master/AspnetIdentitySample

Stunt
  • 1,374
  • 2
  • 10
  • 18
  • Why does this work? I had the same issue as you and I only originally put this line in my web.config because the MSDN tutorial told me to. – John Shedletsky Jun 29 '15 at 21:28
0

Create database on start up of the application. Add the following in the Global.ascx with your dbcontext.

using (FooContext db = new FooContext()) 
        {
            db.Database.CreateIfNotExists();
        }
Purushoth
  • 2,673
  • 3
  • 22
  • 36