1

I'm trying to make an ASP.net MVC website. I have setup Roles in the Startup.cs file as follows:

    private void CreateRolesAndUsers()
    {
        ApplicationDbContext context = new ApplicationDbContext();
        var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
        var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

        if (!roleManager.RoleExists("SuperAdmin"))
        {
            // first we create Admin role   
            var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
            role.Name = "SuperAdmin";
            roleManager.Create(role);
            //Here we create a Admin super user who will maintain the website
            var user = new ApplicationUser();
            user.UserName = "SuperAdmin";
            user.Email = "SuperAdmin@SU.com";

            string userPWD = "Password1";

            var chkUser = UserManager.Create(user, userPWD);
            //Add default User to Role Admin   

            if (chkUser.Succeeded)
            {
                var result1 = UserManager.AddToRole(user.Id, "SuperAdmin");
            }
        }
    }

I have verified that the above code successfully adds the roles to the db and I first noticed the roles not working when I tried Roles.IsUserInRole(User.Identity.Name, "SuperAdmin")

I'm trying to controll which roles see certain content in a view and am getting an Exception:

System.Configuration.Provider.ProviderException: 'The Role Manager feature has not been enabled.'

when I try to call the following line in a .cshtml file: @Roles.GetRolesForUser();

ive looked where to enable roleManager in Web.config, but cant find it and it dosent work if i add it under the system.web section

EDIT: adding (>roleManager enabled="true"<) into the following code produces another unhandled exception whenever using IsUserInRole: "System.Web.HttpException: Unable to connect to SQL Server database."

<system.web>
    <authentication mode="None"/>
    <compilation debug="true" targetFramework="4.7"/>
    <httpRuntime targetFramework="4.7"/>
</system.web>

i think i might be simply missing a dependancy or some initalizer but have no clue and none of the other solutions in similar quesions have worked.

Wboston
  • 11
  • 3
  • UPDATE: for the time being, I've found a workaround. by using "this.User.IsInRole("SuperAdmin")" I get a working return value. but the above issue still doesn't work. – Wboston Nov 19 '18 at 18:10

1 Answers1

1

You can do this by reading from the boolean property at:

System.Web.Security.Roles.Enabled This is a direct read from the enabled attribute of the roleManager element in the web.config:

<configuration>
  <system.web>
    <roleManager enabled="true" />
  </system.web>
</configuration>

For more information, check out this MSDN sample: https://msdn.microsoft.com/en-us/library/aa354509(v=vs.110).aspx

RemS
  • 272
  • 1
  • 9
  • 1
    as I said in my comment above, I've tried that and it doesn't work. it takes some time to compile and run, then eventually throws another exception : System.Web.HttpException: 'Unable to connect to SQL Server database.' – Wboston Nov 19 '18 at 07:52
  • Have you verified your SQL connection string..Are you able to connect to SQL server database? – RemS Nov 19 '18 at 08:32
  • let's put it this way, everything works before I tried accessing the roleManager, and with your suggested fix, I'd have to recreate and reconnect to the database... that doesn't quite make sense, but thanks for your tip – Wboston Nov 19 '18 at 18:05