2

When i put this in my Index.cshtml:

   @if (Request.IsAuthenticated && User.IsInRole("Admin"))
                {
                    <li><a href="#">Gerenciar</a></li>
                }

It throws this error:

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

Additional information:

It is not possible to connect with the SQL Server database

Everything is working SQL Server related, it creates the database, create users fine.

I already looked at other questions and there is no answer that helps!

UPDATE 1

I initialize my context using the default Identity 2.0 context:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    static ApplicationDbContext()
    {
        // Set the database intializer which is run once during application start
        // This seeds the database with admin user credentials and admin role
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<Gatos.Models.Gato> Gato { get; set; }

    public System.Data.Entity.DbSet<Gatos.Models.Formulario> Formulario { get; set; }
    public System.Data.Entity.DbSet<Gatos.Models.Imagem> Imagem { get; set; }
}

My connection string:

<add name="DefaultConnection" connectionString="Data Source=Bruno-PC;Initial Catalog=Gatos;User Id = sa; Password = *******" providerName="System.Data.SqlClient" />

Update 2

Now my:

[Authorize(Roles="Admin")]

in my controllers are throwing the same error! This is driving me insane!

Bruno Xavier
  • 377
  • 1
  • 16
  • 1
    How do you initialize your IdentityDbContext ? Is it picking up the correct connection string? Are you able to use `User.IsInRole("Admin")` without any problems in other part of solution (for example inside a controller action)? – zed May 07 '15 at 18:26
  • How would I do it in a controller? Can you give me an example to test? – Bruno Xavier May 07 '15 at 19:04
  • 1
    @BrunoXavier the same code should work (or fail the same way) within a controller action: `if (User.IsInRole("Admin"))` – David Tansey May 07 '15 at 19:20
  • An alternate test would be to decorate the controller action with this attribute: `[Authorize(Roles = "Admin")]` – David Tansey May 07 '15 at 19:21
  • 1
    I have had this issue before and adding this to my web.config resolved it for me: [http://stackoverflow.com/a/22441007/636942](http://stackoverflow.com/a/22441007/636942) – Brad C May 07 '15 at 20:25
  • Your view code could be executed after the database connection is closed. So if IsInRole() is causing a database query to be ran, I suggest moving that to your controller action and storing the result in the ViewBag and then have the view look at what's in the ViewBag. – Skye MacMaster May 07 '15 at 20:32
  • Thanks Scott, used your approach and it worked! – Bruno Xavier May 07 '15 at 22:17

2 Answers2

2

Are you using a custom Membership for authentication / Authorization?

As a Shortcut, try removing the following line from your main Web.Config:

<roleManager enabled="true" />

Also, take a look at the following page regarding this issue and the solution:

MVC 5 - RoleManager

David
  • 239
  • 3
  • 10
1

Try setting your password in a connection string instead of asterisk (*)

<add name="DefaultConnection" connectionString="Data Source=Bruno-PC;Initial Catalog=Gatos;User Id = sa; Password = your_db_pwd" providerName="System.Data.SqlClient" />
Shreyas Pednekar
  • 1,285
  • 5
  • 31
  • 53